开发者

php regex parsing, keep the contents of the tag intact

开发者 https://www.devze.com 2023-03-29 02:12 出处:网络
input string: <sometag> valid content ..... ....line breaksome开发者_如何学Go more valid content</sometag>

input string:

<sometag> valid content .....
....line break    some开发者_如何学Go more valid content</sometag>

output string:

    valid content.....
......line break some more valid content

Please let me know how to do it, thanks.


You can use the strip_tags() PHP function instead of a regular expression. See manual.

<?php
  $str = "<sometag> valid content .....
  ....line break    some more valid content</sometag>";
  echo strip_tags($str);

  //valid content .....
  //....line break    some more valid content
?>


$string = '<sometag> valid content .....
....line break    some more valid content</sometag>';

$string = preg_replace('/<[^>]+>/', '', $string);


$oldstring = "<sometag> valid content ..... ....line break    some more valid content</sometag>";

$newstring = preg_replace('/\b<sometag>\b/', '', $oldstring); 
$newstring = preg_replace('/\b</sometag>\b/', '', $newstring);
$newstring = preg_replace('<br/>', '', $newstring);

assuming by line break you mean <br/>

0

精彩评论

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

关注公众号