Page 1 of 1

Preg_replace keeping wildcards?

Posted: Mon Mar 14, 2011 8:27 am
by SkyNT
Hello,

I am trying to parse $lan_content (which contains HTML) for hyperlinks and tack on the string 'lan='.$language at the end of the hyperlink URL. I am not sure how to go about replacing the wildcards properly. At the same time I would like to know if my regex patern makes sense (assuming it will do this for all index.php pages. There's alot going on here and I am not very experienced with regex.

I expext a hyperlink to be declared either:
href="index.php?_variables_"
href="/index.php?_variables_"
href='index.php?_variables_'
href='/index.php?_variables_'

Code: Select all

preg_replace('#href=".?index.php?.*"#', '#href=".?index.php?.*lan='.$language.'"#', $lan_content);

Re: Preg_replace keeping wildcards?

Posted: Mon Mar 14, 2011 12:52 pm
by AbraCadaver
Won't work if there is not a query string ( ?_variables_ ):

Code: Select all

$new = preg_replace('#(href=([\'"]).*?index\.php\?.*?)[\'"]#', "$1&lan=$language$2", $lan_content);

Re: Preg_replace keeping wildcards?

Posted: Tue Mar 15, 2011 10:26 am
by SkyNT
Ah yes that worked really well, thank you kind sir!

I noticed you are inserting '&lan='.$language between variables $1 and $2. $1 represents (href=([\'"]).*?index\.php\?.*?) and $2 represents [\'"] I presume? Interesting, didn't know you could do that.

Re: Preg_replace keeping wildcards?

Posted: Tue Mar 15, 2011 10:36 am
by AbraCadaver
Those are back-references. They reference a capture group ( ) in order of appearance so: ($1 ($2) )