Author Topic: very simple regex question for someone who just cant code  (Read 414 times)

0 Members and 1 Guest are viewing this topic.

Offline bertomTopic starter

  • Irregular
  • Posts: 3
    • View Profile
very simple regex question for someone who just cant code
« on: March 26, 2010, 07:20:56 AM »
Hello,
I was hoping to find an answer here on this question:

I need to convert some urls dynamically with php:
http://www.domain.com/something-variable/12345/
into
http://www.domain.com/something-variable/12345/?xx=yy

the last two parts of the url are variable, and as you can see, the the url needs a variable string attached at the end. (always the same). I told you it was easy.

I found out that this could be done through a preg_replace command, and I tried, but i have no idea how to fix this.
I am not a developer you see.

Any help is appreciated.

Thanks,
Bert


Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: very simple regex question for someone who just cant code
« Reply #1 on: March 26, 2010, 07:44:41 AM »
Roughly speaking something like this...

$output preg_replace('#http://www\.domain\.com/([a-z0-9-]+)/([0-9]+)/#i''http://www.domain.com/$1/$2/?xx=yy'$input);
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline bertomTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: very simple regex question for someone who just cant code
« Reply #2 on: March 26, 2010, 08:03:12 AM »
Thanks for the answer Cags,
I'll give that a try.
In your example, what would the #i, the $1 and $2 stand for?
Just trying to understand.

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: very simple regex question for someone who just cant code
« Reply #3 on: March 26, 2010, 08:05:20 AM »
The i makes the pattern case insensitive. The $1 and $2 represent capture groups one and two. Basically it will insert in their places the values captured in the pattern between the corresponding set of brackets. So $1 will be the first directory in the URI and $2 it's child directory.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline bertomTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: very simple regex question for someone who just cant code
« Reply #4 on: March 26, 2010, 08:07:50 AM »
Well then this must be exactly what I need.
Thanks a lot.