tags:

views:

24

answers:

1

I have an array of URLs I'm running thru an included 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.

A: 

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
}
NullUserException
Before I placed it in there it wouldn't even run due to an inability to redeclare function error.You want the actual regex and all? Like I said, the code works as intended when used once in individual scripts, it's multiple uses within the same script that are the problem here, namely it keeps returning the first result over and over.
Clarissa
@Clarissa That's why you don't put function declarations in loops
NullUserException
Hmmm, it's not working for me, still get a cannot redeclare error. I think I'll go ahead and post the gist of my code so you can better appreciate what I'm trying to do. $urls = array(); foreach ($urls as $url) { $ch = curl_init(); ---cURL code here--- foreach ($abc[0] as $xyz) { ---more code here--- $bar = --input defined here-- function name($match) { return($match[1]); } $foo = preg_replace_callback("#[regex]#", "name", $bar); // do stuff with $foo } }Hope this helps, what is comes down to is being able to recycle.
Clarissa
Oops, code didn't format right, sorry.
Clarissa
I edited my original post to include the above code.
Clarissa