使用php在post_content中用增量值替换str_replace [英] str_replace in post_content with increment value using php

查看:30
本文介绍了使用php在post_content中用增量值替换str_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$selected_publications = $post->post_content;

$ii = 0;

$getLi = array("<li>","</li>");

$replaceLi   = array("<li><div>$ii</div><p>","</p></li>"); 

$selectedPublications = str_replace($getLi, $replaceLi, $selected_publications);

echo $selectedPublications;

由于这个原因有不可数的 li 标签,这个循环 for($ii=0; $ii <= 10; $ii++) 被限制为 10 li 标签,我认为如果 li 标签5 ,它会循环 5 次.如果 15 li tag ,它将循环 15 次.我认为 for($ii=0; $ii <= 10; $ii++) 不是我问题的实际解决方案.

There has uncountable li tag for this reason this loop for($ii=0; $ii <= 10; $ii++) is limited for 10 li tag, I think that If li tag 5 , it will loop 5 times. if 15 li tag , it will 15 times loop. I think for($ii=0; $ii <= 10; $ii++) is not actual solution for my problem.

推荐答案

$selected_publications = $post->post_content;
$pieces = explode("</li>", $selected_publications); 
$selectedPublication[]='';  
for($i=0;$i<count($pieces);$i++){       
$getLi = array("<li>","</li>");
$ii = $i+1;
$replaceLi   = array("<li><div>$ii</div><p>","</p></li>");              
$selectedPublication[$i] = str_replace($getLi, $replaceLi, $pieces[$i]);
    echo $selectedPublication[$i];      
}

我已经解决了上述问题.

I have solved my above problem.

这篇关于使用php在post_content中用增量值替换str_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆