标签: preg-replace-callback

在preg_replace_callback中使用局部变量 - PHP

如何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 preg-replace-callback

1
推荐指数
1
解决办法
1898
查看次数

如何知道preg_replace_callback中的匹配计数 - PHP

我在php中有这个代码 - :

function pregRepler($matches)
{
    * do something
}

$str = preg_replace_callback($reg_exp,'pregRepler',$str);
Run Code Online (Sandbox Code Playgroud)

在功能上pregRepler,我想知道当前的情况match number,如果它是第一场比赛还是第二场比赛......

我该怎么做.??

php preg-replace-callback

1
推荐指数
1
解决办法
1907
查看次数

preg_replace_callback语法适用于PHP 5.3,但不适用于PHP 5.2

这是一个小的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)

php preg-replace-callback php-5.2 php-5.3

1
推荐指数
1
解决办法
1023
查看次数

为什么 preg_replace_callback 传递给回调函数的参数是一个数组

这可能是一个愚蠢的问题,但为什么preg_replace_callback将每个匹配项放入单元素数组中并将其传递给回调函数,所以我必须使用它$matches[0]来处理匹配项,为什么它不将匹配项作为字符串传递给回调函数?

php regex string preg-replace-callback

1
推荐指数
1
解决办法
199
查看次数

从回调内更新preg_replace_callback之外的变量

我有一个看起来像这样的功能......

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.

知道如何在回调中改变这个吗?

谢谢

php preg-replace-callback

1
推荐指数
1
解决办法
458
查看次数

RegEx,preg_replace_callback问题PHP

这就是我为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)

php regex preg-replace preg-replace-callback

0
推荐指数
1
解决办法
168
查看次数

preg_replace_callback()中的第二个参数

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数组

我希望你能帮帮我!

php parameters preg-replace-callback

0
推荐指数
1
解决办法
3127
查看次数

不推荐使用:preg_replace():不推荐使用/ e修饰符,使用preg_replace_callback

我无法修复此错误:

$match[1] = preg_replace('/(?<=^|[a-z])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
Run Code Online (Sandbox Code Playgroud)

怎么改呢?

php preg-replace-callback

0
推荐指数
1
解决办法
160
查看次数

如何使用正则表达式捕获嵌套的{%if ...%} {%endif%}语句

这就是我现在所得到的:

/{% 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%}语句,如果有,怎么样?

php regex preg-replace-callback

-2
推荐指数
1
解决办法
545
查看次数