我想要匹配的替换模式

时间:2022-09-13 07:44:41

i am trying to replace string that is matched see example bellow

我正在尝试替换匹配的字符串,请参阅下面的示例

<?php
    $str="this is going to bold [[this]]";
    echo preg_replace("/[[(.*)]]+/i","<b>$1</b>",$str);
?>

So the output will look like this

所以输出看起来像这样

this is going to bold this

这将是大胆的

Edit:

<?php
    $str="bhai bhai *that* -wow- perfect";
    $find[0]="/*(.+)*/i";
    $find[1]="/-(.+)-/i";
    $rep[0]="<b>$1</b>";
    $rep[1]="<i>$1</i>";
    echo preg_replace($find,$rep,$str);
?>

This is showing warning

这显示出警告

Warning: preg_replace() [function.preg-replace]: Compilation failed: nothing to repeat at offset 0 in C:\xampp\htdocs\page.php on line 7

警告:preg_replace()[function.preg-replace]:编译失败:在第7行的C:\ xampp \ htdocs \ page.php中偏移0处不重复

3 个解决方案

#1


4  

Try this :

尝试这个 :

<?php
    $str="this is going to bold [[this]]";
    echo preg_replace("/\[\[(.+)\]\]+/i","<b>$1</b>",$str);
?>

Output :

this is going to bold this

这将是大胆的


Hint :

[ and ] characters are considered special, so you'll have to escape them (like : \[, \]).

[和]字符被认为是特殊的,因此您必须将它们转义(例如:\ [,\])。


UPDATE :


<?php
    $str="bhai bhai *that* -wow- perfect";
    $find[0]="/\*(.+)\*/i";
    $find[1]="/\-(.+)\-/i";
    $rep[0]="<b>$1</b>";
    $rep[1]="<i>$1</i>";
    echo preg_replace($find,$rep,$str);
?>

#2


0  

Try this on for size

尝试这个尺寸

<?php
    $str="this is going to bold [[this]]";
    echo preg_replace("/(?:\[\[)(.*?)(?:\]\])/i","<b>$1</b>",$str);
?>

#3


0  

You need to escape the square brackets in your regular expression. The final expression should look something like this:

您需要转义正则表达式中的方括号。最终的表达式应如下所示:

echo preg_replace('/\[\[(.*?)\]\]/im', '<b>$1</b>', $str);

The square brackets are special characters used for defining a set of characters and thus must be escaped if you wish to match them literally.

方括号是用于定义一组字符的特殊字符,因此如果您希望按字面意思匹配它们,则必须进行转义。

#1


4  

Try this :

尝试这个 :

<?php
    $str="this is going to bold [[this]]";
    echo preg_replace("/\[\[(.+)\]\]+/i","<b>$1</b>",$str);
?>

Output :

this is going to bold this

这将是大胆的


Hint :

[ and ] characters are considered special, so you'll have to escape them (like : \[, \]).

[和]字符被认为是特殊的,因此您必须将它们转义(例如:\ [,\])。


UPDATE :


<?php
    $str="bhai bhai *that* -wow- perfect";
    $find[0]="/\*(.+)\*/i";
    $find[1]="/\-(.+)\-/i";
    $rep[0]="<b>$1</b>";
    $rep[1]="<i>$1</i>";
    echo preg_replace($find,$rep,$str);
?>

#2


0  

Try this on for size

尝试这个尺寸

<?php
    $str="this is going to bold [[this]]";
    echo preg_replace("/(?:\[\[)(.*?)(?:\]\])/i","<b>$1</b>",$str);
?>

#3


0  

You need to escape the square brackets in your regular expression. The final expression should look something like this:

您需要转义正则表达式中的方括号。最终的表达式应如下所示:

echo preg_replace('/\[\[(.*?)\]\]/im', '<b>$1</b>', $str);

The square brackets are special characters used for defining a set of characters and thus must be escaped if you wish to match them literally.

方括号是用于定义一组字符的特殊字符,因此如果您希望按字面意思匹配它们,则必须进行转义。