22

I'm sorry to ask a question but I am useless when it comes to understanding regex code.

In a php module that I didn't write is the following function

function isURL($url = NULL) {
    if($url==NULL) return false;

    $protocol = '(http://|https://)';
    $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

    $regex = "^". $protocol . // must include the protocol
                     '(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars
                     '[a-z]' . '{2,6}'; // followed by a TLD
    if(eregi($regex, $url)==true) return true;
    else return false;
}

Can some kind soul give me the replacement code for that with whatever is required to replace the eregi

hakre
  • 193,403
  • 52
  • 435
  • 836
Colin
  • 231
  • 1
  • 2
  • 7
  • What is the purpose of replacing it? – William Isted Mar 31 '12 at 07:50
  • 3
    @William, functions like `ereg`, `eregi`, `split` etc. are deprecated (not only deprecated, but completely removed) as of PHP 5.3. [Read more](http://www.php.net/manual/en/reference.pcre.pattern.posix.php). – Tomas Mar 31 '12 at 08:00
  • Replacement altogether? http://stackoverflow.com/questions/9954064/ereg-eregi-replacement/9954148#9954148 – William Isted Mar 31 '12 at 08:08
  • @William, but this solution is too localized to this particular case... if he is porting some code to PHP 5.3, he needs more general solution. – Tomas Mar 31 '12 at 08:18

4 Answers4

49

Good question - this is needed when you upgrade to PHP 5.3, where ereg and eregi functions are deprecated. To replace

eregi('pattern', $string, $matches) 

use

preg_match('/pattern/i', $string, $matches)

(the trailing i in the first argument means ignorecase and corresponds to the i in eregi - just skip in case of replacing ereg call).

But be aware of differences between the new and old patterns! This page lists the main differences, but for more complicated regular expressions you have to look in more detail at the differences between POSIX regex (supported by the old ereg/eregi/split functions etc.) and the PCRE.

But in your example, you are just safe to replace the eregi call with:

if (preg_match("%{$regex}%i", $url))
    return true;

(note: the % is a delimiter; normally slash / is used. You have either to ensure that the delimiter is not in the regex or escape it. In your example slashes are part of the $regex so it is more convenient to use different character as delimiter.)

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • 2
    many thanks Tomas you are a star. – Colin Mar 31 '12 at 08:27
  • sorry Tomas your solution gives an error preg_match() [function.preg-match]: Unknown modifier 'h' is that being caused by the i after the } – Colin Mar 31 '12 at 08:44
  • @Colin, ok, I see! This is because the delimiter `|` is used in the $regex also... so we have to use different delimiter, which is not present in the $regex, e.g. `%` (hope I haven't overlooked something again :-)). Updated my answer. – Tomas Mar 31 '12 at 08:47
  • many thanks that seems to have done the trick. One final question. eregi ignores case distinction when matching alphabetic characters. Does the code that I have now still do that or do I need to add anything more to what is in $allowed ? – Colin Mar 31 '12 at 09:10
  • @Colin, no, the ignorecase option is the trailing "i" you see at the end of the pattern parameter to `preg_match`. This new code should be completely equivalent to the old one. – Tomas Mar 31 '12 at 09:12
  • thank you once again. I really appreciate the help you have given me and your explanations. – Colin Mar 31 '12 at 09:31
  • The [doc](http://php.net/manual/en/function.eregi.php) says _(with the i (PCRE_CASELESS) modifier_ what does it mean ? – Gautier Feb 08 '18 at 09:32
15

Palliative PHP 5.3 until you replace all deprecated functions

if(!function_exists('ereg'))            { function ereg($pattern, $subject, &$matches = []) { return preg_match('/'.$pattern.'/', $subject, $matches); } }
if(!function_exists('eregi'))           { function eregi($pattern, $subject, &$matches = []) { return preg_match('/'.$pattern.'/i', $subject, $matches); } }
if(!function_exists('ereg_replace'))    { function ereg_replace($pattern, $replacement, $string) { return preg_replace('/'.$pattern.'/', $replacement, $string); } }
if(!function_exists('eregi_replace'))   { function eregi_replace($pattern, $replacement, $string) { return preg_replace('/'.$pattern.'/i', $replacement, $string); } }
if(!function_exists('split'))           { function split($pattern, $subject, $limit = -1) { return preg_split('/'.$pattern.'/', $subject, $limit); } }
if(!function_exists('spliti'))          { function spliti($pattern, $subject, $limit = -1) { return preg_split('/'.$pattern.'/i', $subject, $limit); } }
Nathan
  • 8,093
  • 8
  • 50
  • 76
Roger Wolff
  • 159
  • 1
  • 3
1

Did you want a complete replacement to preg_match and eregi?

if(!filter_var($URI, FILTER_VALIDATE_URL))
{ 
return false;
} else {
return true;
}

Or for Email:

if(!filter_var($EMAIL, FILTER_VALIDATE_EMAIL))
{ 
return false;
} else {
return true;
}
William Isted
  • 11,641
  • 4
  • 30
  • 45
0

eregi is depreciated in PHP you have to use preg_match

function isValidURL($url)
{
    return preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url);
}


if(isValidURL("http://google.com"))
{
    echo "Good URL" ;
}
else
{
    echo "Bad Url" ;
}

Please see http://php.net/manual/en/function.preg-match.php for more information Thanks

:)

Baba
  • 94,024
  • 28
  • 166
  • 217
  • Your replacement regex does not apply the same rules – meouw Mar 31 '12 at 07:54
  • hey down voter .. do you care to give reasons why i was down voated ??? – Baba Mar 31 '12 at 07:55
  • @meouw ...the rule is to verify valid email address ... am i missing anything – Baba Mar 31 '12 at 07:56
  • The question is about replacing the ereg regular expression with something equivalent - yours is not equivalent - yours has an optional protocol part, for example, wheras the original requires the protocol specifically – meouw Mar 31 '12 at 07:59
  • @meouw someone just gave a none regex example and did not get down voted ... All i gave is is a replacement for url validation .. I think you should have left Colin to decide if its relevant to him or now .. i don't have a wrong code ... it might not be 100% but that does not mean i should be down voted – Baba Mar 31 '12 at 08:04
  • Thanks I really appreciate your replies. My host has just upgraded to PHP/5.3.10 so the replacement is needed. The only thing I don't see in the solution provided by Baba is what the original function had of checking 1 or several sub domains with a max of 63 chars. – Colin Mar 31 '12 at 08:18
  • @Colin ... seen ... am happy we where able to help .... – Baba Mar 31 '12 at 08:34