开发者

PHP preg_replace remove first HTML element in string

I would like to remove the entire first element of a html string (it is always a paragraph) in PHP.

my current approach is using:

$passage = preg_replace('/.*?\b'.'</p>'.'\b/s', '', $passage, 1);

This doesn't work because of the special characters in </p>

I know that the following will remove everything from the string before the word 'on开发者_如何学JAVAe' appears

$passage = preg_replace('/.*?\b'.'one'.'\b/s', '', $passage, 1);


You have to escape '/' with a backslash if you're using it as a delimiter: So it's <\/p>

Edit: You should add a ^ to mark the start of the string.

Another solution: You can use other delimiters like #.

Full code $passage = preg_replace('#^.*?</p>#is', '', $passage, 1);


you can use this regExp
$passage = preg_replace('/^<p>\s*(.+?)\s*</p>$/is','$1', $passage, 1);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜