3

This is a follow-up question to the one I posted here (thanks to mario)

Ok, so I have a preg_replace statement to replace a url string with sometext, insert a value from a query string (using $_GET["size"]) and insert a value from a associative array (using $fruitArray["$1"] back reference.)

Input url string would be:

http://mysite.com/script.php?fruit=apple

Output string should be:

http://mysite.com/small/sometext/green/

The PHP I have is as follows:

$result = preg_replace('|http://www.mysite.com/script.php\?fruit=([a-zA-Z0-9_-]*)|e', ' "http://www.mysite.com/" .$_GET["size"]. "/sometext/" .$fruitArray["$1"]. "/"', $result);

This codes outputs the following string:

 http://mysite.com/small/sometext//

The code seems to skip the value in $fruitArray["$1"].

What am I missing?

Thanks!

3
  • 1
    When asking such a question, you should entirely remove the $_GET and $fruitArray and use constants instead, to localize the problem to the preg_replace only. Maybe your variables are empty, we can't know it for sure Commented May 12, 2011 at 17:23
  • @Riccardo Galli You are quite correct there, lesson learnt - must stop writing php on the back of a fag packet and move to a PHP IDE - do you have any recommendations? I'm such a noob :/ Cheers. Commented May 12, 2011 at 19:23
  • Managed to solve this by re-writing some of the preceding code; I had other preg_replace calls interfering with the url string :/ Perhaps my code above was semi-OK after all :-) This project started small but now its hundreds and hundreds of lines of code - Apologies to all - I do appreciate the answers though, cheers! Commented May 12, 2011 at 19:28

4 Answers 4

1

Well, weird thing.

Your code work's perfectly fine for me (see below code that I used for testing locally).

I did however fix 2 things with your regex:

  1. Don't use | as a delimiter, it has meaning in regex.
  2. Your regular expression is only giving the illusion that it works as you're not escaping the .s. It would actually match http://www#mysite%com/script*php?fruit=apple too.

Test script:

$fruitArray = array('apple' => 'green');
$_GET = array('size' => 'small');
$result = 'http://www.mysite.com/script.php?fruit=apple';
$result = preg_replace('@http://www\.mysite\.com/script\.php\?fruit=([a-zA-Z0-9_-]*)@e', ' "http://www.mysite.com/" .$_GET["size"]. "/sometext/" .$fruitArray["$1"]. "/"', $result);
echo $result;

Output:

Rudis-Mac-Pro:~ rudi$ php tmp.php
http://www.mysite.com/small/sometext/green/

The only thing this leads me to think is that $fruitArray is not setup correctly for you.


By the way, I think this may be more appropriate, as it will give you more flexibility in the future, better syntax highlighting and make more sense than using the e modifier for the evil() function to be internally called by PHP ;-) It's also a lot cleaner to read, IMO.

$result = preg_replace_callback('@http://www\.mysite\.com/script\.php\?fruit=([a-zA-Z0-9_-]*)@', function($matches) {
        global $fruitArray;
        return 'http://www.mysite.com/' . $_GET['size'] . '/sometext/' . $fruitArray[$matches[1]] . '/';
}, $result);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your detailed answer and for pointing out better ways of doing this; it's appreciated. I'm off to have a go at preg_replace_callback, something new for me, cheers :)
No problem! Glad I could help, also relieved to hear it wasn't this code you posted as it was confusing me that it would work for me and not you ;-)
1

i write it again, i don't understand good where is the error, the evaluation of preg results is very weird in php

preg_replace(
    '|http\://([\w\.-]+?)/script\.php\?fruit=([\w_-]+)|e'
    , '"http://www.$1/".$_GET["size"]."/sometext/".$fruitArray["$2"]."/";'
    , $result
);

2 Comments

thanks for your effort, I managed to resolve this now though. regex is so powerful yet so hard to master :/ Cheers!
@Matt regexp are powerful as you say, but sometimes is hard to be human readable even if you write it, and is a good practice write it again. Regexps are just practice... keep in that way a regexp question is always a nifty question.
0

It looks like you have forgotten to escape the ?. It should be /script.php\?, with a \? to escape properly, as in the linked answer you provided.

1 Comment

whoops, I just forgot to do that in the example posted here; the actual code is escaped properly. Code updated, thanks for pointing it out - problem remains.
0

$fruitArray["\$1"] instead of $fruitArray["$1"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.