Php 根据顺序删除所有段落(在第二、第三或第四段之后…)
我知道了Php 根据顺序删除所有段落(在第二、第三或第四段之后…),php,preg-replace,paragraph,Php,Preg Replace,Paragraph,我知道了 $description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>' $description='text1text2text3text4textn' 我只想删除text3 我的结果是: $description = '<p>text1</p><p
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>'
$description='text1text2text3text4textn'
我只想删除text3
我的结果是:
$description = '<p>text1</p><p>text2</p><p>text3</p>'
$description='text1text2text3'
我猜我们需要用一些正则表达式来替换preg\u
,但我无法编写一个有效的
function str_occurance($needle, $haystack, $occurance) {
$occurance += 2;
$arr = explode($needle, $haystack, $occurance);
unset($arr[0]);
$arr = array_values($arr);
$key = count($arr) - 1;
unset($arr[$key]);
$str = $needle . implode($needle, $arr);
return $str;
}
不是最漂亮的,但它很管用
编辑:要使用:
$description = '<p>text1</p><p>text2</p><p>text3</p><p>text4</p><p>textn</p>';
$split = '<p>';
$return = 3;
$new = str_occurance($needle, $description, $return);
echo $new; // returns <p>text1</p><p>text2</p><p>text3</p>
$description='text1text2text3text4textn;
$split='';
$return=3;
$new=str_发生($needle,$description,$return);
echo$new;//返回text1text2text3
快到2013年了。使用XML解析器。切勿使用正则表达式解析XML。在未来的某个地方,你可能会导致一些随机开发人员的精神崩溃。。。或者在过去。看,正则表达式可能不是这项工作的最佳工具,但我认为如果你的标记是统一的,那么正则表达式就可以工作。你没有给我们文档的结构,所以很难找到正则表达式。我不知道preg语法,但是如果p
标记未关闭(这是有效的HTML)或包含其他标记,则类似于([^@User)的内容将失败(根本不做任何事情)。