Jump to content

How to convert ereg_replace('^http://', '', $url)) to a preg_replace expression


Recommended Posts

Hi, this is my first post. As a newbie I am I can't find the correct preg expression to convert a deprecated ereg_replace('^http://', '', $url)) to the correct preg_replace php expression.

Can help me?

 

ereg_replace('^http://', '', $url)) -> preg_replace(???????,'',$url)

 

I have tried without succes preg_replace('/b^http://b/', '', $url) and preg_replace('/^http:///', '', $url)

preg_replace('~\bhttp://~i', '', $url);

 

This is not a direct translation. For one thing, you replaced the start of string anchor tag with a word boundary. Those are not the same thing.  Also, you threw in the case-insensitive modifier...why?  ereg_replace is case-sensitive.  eregi_replace is not.  Your translation will now allow for matches like

 

"....http://"

http://"

"]]]]httP://"

"#$htTP://"

etc...

 

Here is a direct translation:

 

preg_replace('~^http://~', '', $url);

 

As a note for migrating from the posix functions (eregxxx) to the pcre functions (pregxxx), 99.99% of the time there is no change needed to the pattern itself.  Basically all you really need to do is throw a delimiter around the pattern (like in this case, ~ was used).  All pregxxx functions must have delimiters around the patterns because pattern modifiers go in the pattern string too.  "~pattern~modifier"  The delimiter can be most any non-alphanumeric character, but  /.../  ~...~ #...# and !...! are the ones I see most commonly used.  Just remember whatever delimiter you choose to use, you must escape it if you need to use that character in the pattern itself (for this reason I avoid using / as delimiter because that comes up a lot in parsing html. For example, if / was used on your pattern, it would have looked like this: '/^http:\/\//'  not as clear, right?).

 

And for ereg vs. eregi, a pattern modifier is used instead of a separate function (the "i" that JAY added after the ending delimiter). 

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.