开发者

preg_replace_callback stuck

I have an array of URLs I'm running thru an 开发者_如何学Goincluded preg_replace_callback function, the idea being that each loop will yield a new result.

Problem is that it keeps outputting only the first result, as if it stalls after processing the first URL.

Here is the code:

if (!function_exists('name')) {
function name($match)
{
return($match[1]);
}
$foo = preg_replace_callback("#[regex]#", "name", $bar);
}

Any ideas how I can get this to work properly? Thanks.


You can also use T-Regx library:

pattern('[regex]')->replace($bar)->callback('name');


If you are applying the function preg_replace_callback() to all elements in an array, you might want to do this:

// put this on the top of the file
function name($match) {
    return($match[1]);
}

Then, to iterate through elements of an array:

foreach ($array as $value) {
    $foo = preg_replace_callback("#[regex]#", "name", $value);
    // do stuff with $foo
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜