[英]Php replace string with regex
我想在文件中将所有标签“ <_tag_>”替换为“”。
我已经尝试过以下解决方案:
$_text = preg_replace('<\\_\\s*\\w.*?\\_>', '', $_text);
但我将“ <_tag_>”替换为“ <>”
$_text = preg_replace('<\\_(.*?)\\_>', '', $_text);
但我将“ <_tag_>”替换为“ <>”
如何选择尖括号?
它可能是
<_.+?_>
# <_, anything lazily afterwards, followed by _>
在PHP
:
$string = preg_replace('~<_.+?_>~', '', $string);
如
<?php
$string = "some <_tag_> here";
$string = preg_replace('~<_.+?_>~', '', $string);
echo $string;
# some here
?>
在ideone.com上查看演示 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.