高手帮看看我的代码,我想要用php提取内容的多个关键词加并全部加超链接,现在只加了一个超链
?phpheader("Content-type:text/html;charset=gb2312");$content='<divclass="recipeCatego...
?php
header("Content-type: text/html; charset=gb2312");
$content = '<div class="recipeCategory_sub_R">
<ul>
<li><span class="category_s1">韩式辣酱</span><span class="category_s2">(两勺)</span></li>
<li><span class="category_s1">韩式包饭酱</span><span class="category_s2">(两勺)</span></li>
<li><span class="category_s1">雪碧</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">芝麻</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">香油</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">鸡精</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">白糖</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">大蒜</span><span class="category_s2">(半个)</span></li>
</ul>
</div>';
$preg = '/<span class=\"category_s1\">(.*)<\/span>/isU' ; //提取<span class="category_s1"></span>之间的内容
preg_match_all($preg, $content, $arr,PREG_PATTERN_ORDER);
foreach($arr[1] as $url){
$url=trim($url);
$str = preg_replace(
array(
'/'.$url.'/',
),
array(
'<a href=# >'.$url.'</a>',
),
$content,1);
}
echo $str;
?>
我写的代码输出只实现了一个关键词加超链,我想要全部关键词都用替换的形式加超链。 展开
header("Content-type: text/html; charset=gb2312");
$content = '<div class="recipeCategory_sub_R">
<ul>
<li><span class="category_s1">韩式辣酱</span><span class="category_s2">(两勺)</span></li>
<li><span class="category_s1">韩式包饭酱</span><span class="category_s2">(两勺)</span></li>
<li><span class="category_s1">雪碧</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">芝麻</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">香油</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">鸡精</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">白糖</span><span class="category_s2">(适量)</span></li>
<li><span class="category_s1">大蒜</span><span class="category_s2">(半个)</span></li>
</ul>
</div>';
$preg = '/<span class=\"category_s1\">(.*)<\/span>/isU' ; //提取<span class="category_s1"></span>之间的内容
preg_match_all($preg, $content, $arr,PREG_PATTERN_ORDER);
foreach($arr[1] as $url){
$url=trim($url);
$str = preg_replace(
array(
'/'.$url.'/',
),
array(
'<a href=# >'.$url.'</a>',
),
$content,1);
}
echo $str;
?>
我写的代码输出只实现了一个关键词加超链,我想要全部关键词都用替换的形式加超链。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询