简体   繁体   English

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

[英]Php replace string with regex

I want replace in my file all tags "<_tag_>" with "". 我想在文件中将所有标签“ <_tag_>”替换为“”。
I've tried this solutions: 我已经尝试过以下解决方案:

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

  2. $_text = preg_replace('<\\_(.*?)\\_>', '', $_text);
    But I replace "<_tag_>" with "<>" 但我将“ <_tag_>”替换为“ <>”

How can I also select angle brackets? 如何选择尖括号?

It could be 它可能是

<_.+?_>
# <_, anything lazily afterwards, followed by _>

In PHP : PHP

$string = preg_replace('~<_.+?_>~', '', $string);

As in

<?php
$string = "some <_tag_> here";
$string = preg_replace('~<_.+?_>~', '', $string);
echo $string;
# some  here
?>

See a demo on ideone.com . 在ideone.com上查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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