现在,我的第二个问题是,我的脚本无法识别第二个标签。例如,
#heavy / machine gun #test
返回:
<a href="tag.php?id=heavy+">#heavy</a> / machine gun #test
我的代码是:
$words = preg_split("/(#[^\s+[:space:]]+ )/", $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
foreach($words as $var => $value)
{
if($value[0]=='#')
{
$test[$var]='<a href="tag.php?id='.urlencode(substr($value,1)).'"> '.$value.'</a>';
$str=str_replace($value,$test[$var],$descrip);
}
}
第一个#号标签是一个链接,但不是第二个...
预先感谢,杰里米。
<?php
$text = "#heavy / machine gun #test";
$text = preg_replace('/#(\w+)/', '<a href="tag.php?id=$1">#$1</a>', $text);
// or
// $text = preg_replace('/#([^\s+[:space:]]+)/', '<a href="tag.php?id=$1">#$1</a>', $text);
echo $text;
HTML输出
<a href="tag.php?id=heavy">#heavy</a> / machine gun <a href="tag.php?id=test">#test</a>
视觉输出
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句