繁体   English   中英

PHP用正则表达式替换字符串

[英]Php replace string with regex

我想在文件中将所有标签“ <_tag_>”替换为“”。
我已经尝试过以下解决方案:

  1. $_text = preg_replace('<\\_\\s*\\w.*?\\_>', '', $_text);
    但我将“ <_tag_>”替换为“ <>”

  2. $_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM