我对正则表达式很糟糕.我试图取代这个:
public static function camelize($word) {
return preg_replace('/(^|_)([a-z])/e', 'strtoupper("\\2")', $word);
}
Run Code Online (Sandbox Code Playgroud)
使用带有匿名函数的preg_replace_callback.我不明白\\ 2是做什么的.或者就此而言preg_replace_callback的工作原理.
实现这一目标的正确代码是什么?
$result = preg_replace(
"/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/iseU",
"CallFunction('\\1','\\2','\\3','\\4','\\5')",
$result
);
Run Code Online (Sandbox Code Playgroud)
上面的代码在升级到PHP 5.5后给出了弃用警告:
不推荐使用:preg_replace():不推荐使用/ e修饰符,而是使用preg_replace_callback
如何更换代码preg_replace_callback()?
我有以下HTML语句
[otsection]Wallpapers[/otsection]
WALLPAPERS GO HERE
[otsection]Videos[/otsection]
VIDEOS GO HERE
Run Code Online (Sandbox Code Playgroud)
我想要做的是用html div替换[otsection]标签.问题是我想从1-> 2-> 3等增加div的id.
例如,上述声明应该翻译成
<div class="otsection" id="1">Wallpapers</div>
WALLPAPERS GO HERE
<div class="otsection" id="2">Videos</div>
VIDEOS GO HERE
Run Code Online (Sandbox Code Playgroud)
据我所知,最好的方法是通过preg_replace_callback来增加每个替换之间的id变量.但经过1小时的工作后,我无法让它发挥作用.
对此的任何帮助将不胜感激!
我最近将PHP从5.3.27升级到5.5.0.在我的Symfony 2.3.2项目中一切正常,我可以享受最新的PHP功能.
现在当我回到我的其他Symfony 1.4.16项目时,我得到一个关于preg_replace被/ e修饰符弃用的PHP错误.
我在论坛中找不到关于此错误的参考:以前是否有人遇到此问题?有什么样的补丁我可以开箱即用吗?升级到Symfony 1.4.20会解决这个问题吗?
错误消息如下所示:
不推荐使用:preg_replace():不推荐使用/ e修饰符,而是在第409行的/myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php中使用preg_replace_callback
一种方法可能是按照消息和手册中的建议修改代码.如何将preg_replace表达式更改为preg_replace_callback调用?
任何帮助/提示都将非常受欢迎.
编辑:
到目前为止,还没有补丁(Symfony 1.4.20没有解决这个问题).解决方案是将对preg_replace的失败调用替换为在sourche中对preg_replace_callback的相应调用,这很容易在sfWebResponse类中完成(感谢提示Jon).不幸的是,现在下一次失败的情况稍微复杂一些......另一方面,我们可能不得不使用/ e选项grep for preg_replace使用以找出Symfony可能会破坏的位置.这给出了不少结果:o
所以...我的结论是Symfony 1.4用户最好不要将PHP升级到5.5版,直到出现一些严重的补丁.你怎么看 ?还有其他选择
我正在尝试用传递的数组中的值替换{{key}}my中的项目$text.但当我尝试添加print_r以查看发生了什么时,我收到了一个Undefined variable: kvPairs错误.如何在preg_replace_callback?中访问我的变量表?
public function replaceValues($kvPairs, $text) {
$text = preg_replace_callback(
'/(\{{)(.*?)(\}})/',
function ($match) {
$attr = trim($match[2]);
print_r($kvPairs[strtolower($attr)]);
if (isset($kvPairs[strtolower($attr)])) {
return "<span class='attr'>" . $kvPairs[strtolower($attr)] . "</span>";
} else {
return "<span class='attrUnknown'>" . $attr . "</span>";
}
},
$text
);
return $text;
}
Run Code Online (Sandbox Code Playgroud)
更新:
我已经尝试了global范围的东西,但它也不起作用.我添加了两个打印语句来查看最新的内容,一个内部,一个外部preg_replace_callback.
public function replaceValues($kvPairs, $text) {
$attrTest = 'date';
print_r("--" . strtolower($attrTest) . "--" . $kvPairs[strtolower($attrTest)] . "--\n");
$text …Run Code Online (Sandbox Code Playgroud) 我试图使用version_compare在一个文件中支持一些PHP代码的两个版本,但我仍然收到错误.
码:
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
$alias = preg_replace_callback('/&#x([0-9a-f]{1,7});/i', function($matches) { return chr(hexdec($matches[1])); }, $alias);
$alias = preg_replace_callback('/&#([0-9]{1,7});/', function($matches) { return chr($matches[1]); }, $alias);
} else {
$alias = preg_replace('/&#x([0-9a-f]{1,7});/ei', 'chr(hexdec("\\1"))', $alias);
$alias = preg_replace('/&#([0-9]{1,7});/e', 'chr("\\1")', $alias);
}
Run Code Online (Sandbox Code Playgroud)
但我得到:
PHP Parse错误:语法错误,意外T_FUNCTION
在preg_replace_callback()调用上,可能是因为匿名函数.
嗯伙计们,我真的希望我的英语能很好地解释我的需要.
让我们举个例子(这只是一个例子!)代码:
class Something(){
public function Lower($string){
return strtolower($string);
}
}
class Foo{
public $something;
public $reg;
public $string;
public function __construct($reg, $string, $something){
$this->something = $something;
$this->reg = $reg;
$this->string = $string;
}
public function Replace(){
return preg_replace_callback($this->reg, 'Foo::Bar', $this->string);
}
public static function Bar($matches){
/*
* [...]
* do something with $matches and create the $output variable
* [...]
*/
/*
* I know is really useless in this example, but i need to have an …Run Code Online (Sandbox Code Playgroud) php preg-replace preg-replace-callback preg-match-all preg-match
有没有办法通过函数动态替换,类似于.replaceJavaScript中的工作方式?一个类似的用例是我有一个带数字的字符串,我想通过一个函数传递数字:
"1 2 3" => "2 4 6" (x2)
Run Code Online (Sandbox Code Playgroud)
当前preg_replace函数似乎不支持参数的函数.
作为参考,在JavaScript中它可以实现如下:
var str = "1 2 3";
str.replace(/\d+/g, function(num){
return +num * 2;
});
Run Code Online (Sandbox Code Playgroud) 我想找到一种模式{text}并替换包括大括号的文本。
$data = 'you will have a {text and text} in such a format to do {code and code}';
$data= preg_replace_callback('/(?<={{)[^}]*(?=}})/', array($this, 'special_functions'),$data);
Run Code Online (Sandbox Code Playgroud)
我的special function包含回调代码来替换大括号和完全和有条件的文本。
public function special_functions($occurances){
$replace_html = '';
if($occurances){
switch ($occurances[0]) {
case 'text and text':
$replace_html = 'NOTEPAD';
break;
case 'code and code':
$replace_html = 'PHP';
break;
default:
$replace_html ='';
break;
}
}
return $replace_html;
}
Run Code Online (Sandbox Code Playgroud)
预期输出
你将有一个这样格式的记事本来执行 PHP
preg_replace_callback如何使用正则表达式在 php 中同时替换文本和大括号
有人可以帮我解决这个错误吗?
警告:preg_replace():不再支持 /e 修饰符,请改用 preg_replace_callback
我的原始代码:
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
Run Code Online (Sandbox Code Playgroud)
所以我这样试过:
$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./e',
function ($matches) {
foreach ($matches as $match) {
return strtoupper($match);
}
},
strtolower(trim($match[1])));
Run Code Online (Sandbox Code Playgroud)
但我仍然遇到同样的错误:
警告:preg_replace_callback():不再支持 /e 修饰符,请改用 preg_replace_callback