php正则表达式解析,保持标签内容不变

时间:2011-08-19 15:08:00

标签: php regex

输入字符串:

<sometag> valid content .....
....line break    some more valid content</sometag>

输出字符串:

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

请让我知道怎么做,谢谢。

3 个答案:

答案 0 :(得分:4)

您可以使用strip_tags() PHP函数而不是正则表达式。 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
?>

答案 1 :(得分:0)

$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);

假设换行符你的意思是<br/>

答案 2 :(得分:0)

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

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