Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据顺序删除所有段落(在第二、第三或第四段之后…)_Php_Preg Replace_Paragraph - Fatal编程技术网

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='text1

text2

text3

text4

textn

'
我只想删除
text3

我的结果是:

$description = '<p>text1</p><p>text2</p><p>text3</p>'
$description='text1

text2

text3

'
我猜我们需要用一些正则表达式来替换
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='text1

text2

text3

text4

textn

; $split=''; $return=3; $new=str_发生($needle,$description,$return); echo$new;//返回text1

text2

text3


快到2013年了。使用XML解析器。切勿使用正则表达式解析XML。在未来的某个地方,你可能会导致一些随机开发人员的精神崩溃。。。或者在过去。看,正则表达式可能不是这项工作的最佳工具,但我认为如果你的标记是统一的,那么正则表达式就可以工作。你没有给我们文档的结构,所以很难找到正则表达式。我不知道preg语法,但是如果
p
标记未关闭(这是有效的HTML)或包含其他标记,则类似于
([^@User)的内容将失败(根本不做任何事情)。