簡體   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