Home > Backend Development > PHP Tutorial > php preg_replace问题

php preg_replace问题

WBOY
Release: 2016-06-06 20:50:47
Original
1033 people have browsed it

$app_str = "#aa#";
$aa = "test";

$app_str = preg_replace( "/#([^#]+)#/ie", "\\1", $app_str );  
Copy after login
Copy after login

怎样替换成 \\1 对应的变量的值呢

foreach( $_POST as $K => $v ){
// 怎样定义变量 名字 为 $K 值为  $v
}
Copy after login
Copy after login

回复内容:

$app_str = "#aa#";
$aa = "test";

$app_str = preg_replace( "/#([^#]+)#/ie", "\\1", $app_str );  
Copy after login
Copy after login

怎样替换成 \\1 对应的变量的值呢

foreach( $_POST as $K => $v ){
// 怎样定义变量 名字 为 $K 值为  $v
}
Copy after login
Copy after login

我琢磨了半天你的表达 ... 大概理解了一点不知道对不对 ...

如果你的目的是想把 #aa# 这个语法替换成变量 $aa 的值 ...

那么其实你已经离成功很近了 ... 确切说只有一个字符的距离 ...

$app_str = "#aa#";
$aa = "test";

$app_str = preg_replace( "/#([^#]+)#/ie", "$\\1", $app_str );  
Copy after login

但是你的写法有一个问题就是 PREG_REPLACE_EVAL 这个修饰符 ...

也就是 e ... 会在 php 5.5.0 之后被 DEPRECATED ...

所以我的建议是上面的代码虽然可以用 ... 但考虑到未来 ... 还是推荐用 preg_replace_callback() 来实现 ...

示例代码我先不写了 ... 你自己研究一下呗 ...

Related labels:
php
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