如何preg_replace_callback在PHP中使用局部变量.我有以下代码:
function pregRep($matches)
{
global $i; $i++;
if($i > 2)
{
return '#'.$matches[0];
}
else
{
return $matches[0];
}
}
$i = 0;
$str = preg_replace_callback($reg_exp,"pregRep",$str);
Run Code Online (Sandbox Code Playgroud)
而且也是$str一个字符串,$reg_exp是一个正则表达式.这两个都很明确.
谢谢你的帮助.
我在php中有这个代码 - :
function pregRepler($matches)
{
* do something
}
$str = preg_replace_callback($reg_exp,'pregRepler',$str);
Run Code Online (Sandbox Code Playgroud)
在功能上pregRepler,我想知道当前的情况match number,如果它是第一场比赛还是第二场比赛......
我该怎么做.??
这是一个小的PHP示例:
echo '<pre>';
// Execute httpd.exe -V to find apache version
exec('"E:\Program Files\AMPPS\apache\bin\httpd.exe" -V', $out, $ret);
// preg_replace_callback to fetch version
echo $apver = preg_replace_callback('/Server version: Apache\/(.*?) \((.*?)\)/is', function ($matches){ return $apache_version = trim($matches[1]); } ,$out[0]);
echo "\n";
echo "\n";
// Test this file with PHP 5.3
exec('"E:\Program Files\AMPPS\php\php.exe" -l "'.__FILE__.'"', $out1, $ret1);
print_r(array($out1, $ret1));
// Test this file with PHP 5.2
exec('"E:\Program Files\AMPPS\php-5.2\php.exe" -l "'.__FILE__.'"', $out2, $ret2);
print_r(array($out2, $ret2));
Run Code Online (Sandbox Code Playgroud)
输出:
2.4.6
Array
(
[0] => Array
(
[0] => …Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但为什么preg_replace_callback将每个匹配项放入单元素数组中并将其传递给回调函数,所以我必须使用它$matches[0]来处理匹配项,为什么它不将匹配项作为字符串传递给回调函数?
我有一个看起来像这样的功能......
function filterwords($input='poo poo hello world bum trump'){
$report_swear = 0;
$badwords = array('poo','bum','trump');
$filterCount = sizeof($badwords);
for($i=0; $i<$filterCount; $i++){
$input = preg_replace_callback('/\b'.preg_quote($badwords[$i]).'\b/i', function($matches) use ($report_swear) {
$report_swear++;
return str_repeat('*', 4);
}, $input);
}
print_r($report_swear);
return $input;
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我希望$ report_swear变量返回4,但它仍然返回0.
知道如何在回调中改变这个吗?
谢谢
这就是我为RegEx所得到的,我想知道这是否是最佳方式.
我希望能够找到类似的东西,无论标识符之间的间距如何,都不区分大小写.如果可能的话,不用担心订单..
例:
[Foreclosure ="Remax" URL="http://www.remax.com" Title = "4 Bedroom 2 Bath Condo"]
[Foreclosure ="Remax"URL="http://www.remax.com"Title="4 Bedroom 2 Bath Condo"]
[Foreclosure = "Remax" URL="http://www.remax.com" Title = "4 Bedroom 2 Bath Condo" ]
Run Code Online (Sandbox Code Playgroud)
这是我现有的代码:
function ForeclosureCode_filter( $buffer )
{
//There might be a better way to do the regex... But this seems to work...
$buffer = preg_replace_callback( '@\[Forclosure *=*"(.*?)" *url *=*"(.*?)" *title="(.*?)" *\]@si',
"ForeclosureCode_replace", $buffer );
return $buffer;
}
Run Code Online (Sandbox Code Playgroud) 我preg_replace_callback()在PHP中的函数有问题.我想调用一个需要两个参数的函数.
private function parse_variable_array($a, $b)
{
return $a * $b;
}
Run Code Online (Sandbox Code Playgroud)
在互联网上我找到了这段代码:
preg_replace_callback("/regexcode/", call_user_func_array(array($this, "foo"), array($foo, $bar)), $subject);
Run Code Online (Sandbox Code Playgroud)
但是在函数foo中我不能使用通常与preg_replace_callback匹配的matches数组
我希望你能帮帮我!
我无法修复此错误:
$match[1] = preg_replace('/(?<=^|[a-z])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
Run Code Online (Sandbox Code Playgroud)
怎么改呢?
这就是我现在所得到的:
/{% if(.+?) %}(.*?){% endif %}/gusi
它会捕获多个if语句等等.
IMG:http://image.xesau.eu/2015-02-07_23-22-11.png
但是当我做嵌套的那些时,if if in if,它会在{%endif%}的第一次出现时停止
IMG:http://image.xesau.eu/2015-02-08_09-29-43.png
有没有办法像{%if ...%}语句那样捕获尽可能多的{%endif%}语句,如果有,怎么样?