Jump to content

preg replace pattern


mark.d92

Recommended Posts

Hi,

 

I am having an issue understanding how these patterns work. Please can someone provide me the search term for the following issue.

 

I need to find

<base href="?????">

and replace with this

<base href="$home">

 

Thanks for your help. :)

Mark

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.