高手帮看看php代码,我想要提取内容的多个关键词加并全部加超链接,超链接里面有个变量,只循环了一个
我想要用php提取<spanclass="category_s1">xxx</sapn>加并全部加超链接,替换已经完成,但超链接里面有一个变量$wenzi,只循环了一个。...
我想要用php提取<span class="category_s1">xxx</sapn>
加并全部加超链接,替换已经完成,但超链接里面有一个变量$wenzi,只循环了一个。我想要全部对应循环,盼解决。
<?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[0] as $url){
$url=trim($url);
$keys =array(array($url));
if (!isset($str))
$str = $content;
foreach($keys as $nkeys){
for ($i = 0; $i < count($arr[1]); $i++) {
$wenzi=trim($arr[1][$i]);
}
if(strpos($str,$nkeys[0]) ){
$str =str_replace($nkeys[0],"<a data-type=2 biz-keyword=".$wenzi." data-tmpl=290x380 data-tmplid=10 data-rd=2 data-style=1 data-border=1 href=#>".$nkeys[0]."</a>",$str);
}
}
}
echo $str;
?> 展开
加并全部加超链接,替换已经完成,但超链接里面有一个变量$wenzi,只循环了一个。我想要全部对应循环,盼解决。
<?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[0] as $url){
$url=trim($url);
$keys =array(array($url));
if (!isset($str))
$str = $content;
foreach($keys as $nkeys){
for ($i = 0; $i < count($arr[1]); $i++) {
$wenzi=trim($arr[1][$i]);
}
if(strpos($str,$nkeys[0]) ){
$str =str_replace($nkeys[0],"<a data-type=2 biz-keyword=".$wenzi." data-tmpl=290x380 data-tmplid=10 data-rd=2 data-style=1 data-border=1 href=#>".$nkeys[0]."</a>",$str);
}
}
}
echo $str;
?> 展开
2个回答
展开全部
改成这样
foreach($arr[0] as $key => $url){
$url=trim($url);
// $keys =array(array($url));
if (!isset($str))
$str = $content;
//foreach($keys as $nkeys){
//for ($i = 0; $i < count($arr[1]); $i++) {
//$wenzi=trim($arr[1][$i]);
//}
if(strpos($str,$arr[1][$key]) ){
$wenzi = $arr[1][$key];
$str =str_replace($wenzi,"<a data-type=2 biz-keyword=".$wenzi." data-tmpl=290x380 data-tmplid=10 data-rd=2 data-style=1 data-border=1 href=#>".$wenzi."</a>",$str);
//}
}
}
foreach($arr[0] as $key => $url){
$url=trim($url);
// $keys =array(array($url));
if (!isset($str))
$str = $content;
//foreach($keys as $nkeys){
//for ($i = 0; $i < count($arr[1]); $i++) {
//$wenzi=trim($arr[1][$i]);
//}
if(strpos($str,$arr[1][$key]) ){
$wenzi = $arr[1][$key];
$str =str_replace($wenzi,"<a data-type=2 biz-keyword=".$wenzi." data-tmpl=290x380 data-tmplid=10 data-rd=2 data-style=1 data-border=1 href=#>".$wenzi."</a>",$str);
//}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询