Why do that when you can just do the preg_match (or preg_match_all) and be done with it? What's the point of replacing anything? matches[0] will be the entire string found that matches the regex (or an array of entire strings found matching the regex) and matches[1] will be the first selected element (or an array of the first selected element) in the regex and matches[2] will be the second selected element (or an array of the second selected element) of the regex. For example:
$text = 'Blah blah blah [url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]
[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]
[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]
[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]
[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]';
preg_match_all('~\[url=(.*)\].*\[/url\]~iU', $text, $matches);
var_dump($matches);
array_shift($matches);
var_dump($matches);
That will give:
array(2) {
[0]=>
array(5) {
[0]=>
string(77) "[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]"
[1]=>
string(77) "[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]"
[2]=>
string(77) "[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]"
[3]=>
string(77) "[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]"
[4]=>
string(77) "[url=http://somesite.com/files/143699_gpujq/some_file.php]some_file.php[/url]"
}
[1]=>
array(5) {
[0]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[1]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[2]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[3]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[4]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
}
}
array(1) {
[0]=>
array(5) {
[0]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[1]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[2]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[3]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
[4]=>
string(52) "http://somesite.com/files/143699_gpujq/some_file.php"
}
}
What more could you need?