Jump to content

preg replace pattern


mark.d92

Recommended Posts

A quick and dirty regex like

 

/<base\s*href=[\"\'](.*?)[\"\']>/is

 

will match the contents of href so you can replace it. "  .*  " is a greedy expression that basically means select everything possible. "  ?  " defines as few as possible (so it will match only up to the first quote is finds, need this otherwise it wil match up to the last quote which could be 1000 lines down the script such as many strings of "> exist usually)

 

Get yourself a copy of expresso, it did me wonders learning regex with it.

its for windows, you could run it under wine, its not as if you need it constantly. If you dont like wine you can probably find a mac regex builder, but i tried loads of builders for windows and i found expresso to be the best one. Personally preference i guess.

 

What do you mean by wildcard ? The * just means match any, so it will match href "" as well as href="really long string" . You could have used ".+?" and that would have meant "one or more" but if the href was ever empty, it wouldnt match anything. I guess it depends on what your actually trying to accomplish.

 

If by "find out what wildcard matched" you mean return the contents of href, then preg_match would do that. Not sure if theres a return matches in preg_replace, you'd have to check the php docs, its something ive never required to do so im not sure if theres a flag in the function for this or not

 

Hopefully that makes some sort of sense

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.