0

I've been having trouble with a piece of code

<?php

$test = "this is just \"a test\"";

echo "\n$test\n\n";

$tags = explode(' ', preg_replace('/"([^"]+)"/e',
"preg_replace('/\s+/','%20','\\1')", $test));

for($i = 0,$c = count($tags); $i < $c; $i++) {
        if(preg_match('/%20/', $tags[$i])) {
                echo "yes ";
        } else {
                echo "no ";
        }
        preg_replace('/%20/', ' ', $tags[$i]);
}

echo "\n\n";
print_r($tags);

?>

$tags is an array containing ('this','is','just','a%20test'). In the for loop i'm going through each element and trying to change back any instances of %20 to a space but it's not working. I've tried ereg and str replace but I get the same result. Using preg_match with the /%20/ pattern returns true though.

Here's the output:

this is just "a test"

no no no yes

Array
(
    [0] => this
    [1] => is
    [2] => just
    [3] => a%20test
)

Any ideas? Thanks.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by digital-ether
0

Could you explain what you're trying to do?

If you want to use a function all for the replacements, you need to use preg_replace_callback().

Programs actually execute the innermost function first, instead of the outtermost. They just execute each line from top to bottom.

ie:

$tags = explode(' ', preg_replace('/"([^"]+)"/e', preg_replace('/\s+/','%20','\\1'), $test));

is the same as:

$replace = preg_replace('/\s+/','%20','\\1');
$tags = explode(' ', preg_replace('/"([^"]+)"/e', $replace, $test));
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.