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

How to match regular expression with regular expression?

WBOY
Release: 2016-08-04 09:19:26
Original
1068 people have browsed it

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

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

该怎么写这正则表达式?

回复内容:

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

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

该怎么写这正则表达式?

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

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

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

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

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

是完全等价的。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template