简体   繁体   中英

Php replace string with regex

I want replace in my file all tags "<_tag_>" with "".
I've tried this solutions:

  1. $_text = preg_replace('<\\_\\s*\\w.*?\\_>', '', $_text);
    But I replace "<_tag_>" with "<>"

  2. $_text = preg_replace('<\\_(.*?)\\_>', '', $_text);
    But I replace "<_tag_>" with "<>"

How can I also select angle brackets?

It could be

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

In 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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