Home >Backend Development >PHP Tutorial >How to match regular expression with regular expression?

How to match regular expression with regular expression?

WBOY
WBOYOriginal
2016-08-04 09:19:261076browse

在一个长长的字符串中有这么几个东西:
{$var},{$wtf},{$mdzz}
我想通过一个preg_replace把它们分别替换成

<code><?php echo $var;?>
<?php echo $wtf;?>
<?php echo $mdzz;?>
</code>

该怎么写这正则表达式?

回复内容:

在一个长长的字符串中有这么几个东西:
{$var},{$wtf},{$mdzz}
我想通过一个preg_replace把它们分别替换成

<code><?php echo $var;?>
<?php echo $wtf;?>
<?php echo $mdzz;?>
</code>

该怎么写这正则表达式?

使用如下匹配替换:<br>$msg = preg_replace("/{$var}/g", '<?php echo $var;?>', $msg);<br>

单引号' 中的代码不会被解析。

<code>preg_replace('/{\$([a-zA-Z]+)}/','<?php echo \$${1}; ?>',$filestr);</code>

为什么要这样做呢? echo "{$a},{$b},{$c}"

<code>echo $a; 
echo ','; 
echo $b; 
echo ','; 
echo $c;</code>

是完全等价的。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn