lpccoder Posted March 24, 2011 Share Posted March 24, 2011 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) Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted March 24, 2011 Share Posted March 24, 2011 preg_replace('~\bhttp://~i', '', $url); Quote Link to comment Share on other sites More sharing options...
lpccoder Posted March 24, 2011 Author Share Posted March 24, 2011 Thanks a lot!!! Solved!! Quote Link to comment Share on other sites More sharing options...
.josh Posted March 24, 2011 Share Posted March 24, 2011 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.