How to Convert Ereg Expressions to Preg in PHP

How can I convert ereg expressions to preg in PHP?

The biggest change in the syntax is the addition of delimiters.

ereg('^hello', $str);
preg_match('/^hello/', $str);

Delimiters can be pretty much anything that is not alpha-numeric, a backslash or a whitespace character. The most used are generally ~, / and #.

You can also use matching brackets:

preg_match('[^hello]', $str);
preg_match('(^hello)', $str);
preg_match('{^hello}', $str);
// etc

If your delimiter is found in the regular expression, you have to escape it:

ereg('^/hello', $str);
preg_match('/^\/hello/', $str);

You can easily escape all delimiters and reserved characters in a string by using preg_quote:

$expr = preg_quote('/hello', '/');
preg_match('/^'.$expr.'/', $str);

Also, PCRE supports modifiers for various things. One of the most used is the case-insensitive modifier i, the alternative to eregi:

eregi('^hello', 'HELLO');
preg_match('/^hello/i', 'HELLO');

You can find the complete reference to PCRE syntax in PHP in the manual, as well as a list of differences between POSIX regex and PCRE to help converting the expression.

However, in your simple example you would not use a regular expression:

stripos($str, 'hello world') === 0

How to convert ereg() expression to the preg_match() one?

The expression looks fine, but you can simplify it:

^\d{8}\t\d{2}:\d{2}:\d{2}$

Your original one should still work with preg, but the one above is simpler to read and understand. A few notes:

  • [0-9] is the same as \d
  • {1} is unnecessary
  • : does not need escaping

Need to switch from ereg() to preg_match()

Let's break this down:

([0-9]{1,2})

This looks for numbers zero through nine (- indicates a range when used in brackets []) and there can be 1 or two of them.

.

This looks for any single character

([0-9]{1,2})

This looks for numbers zero through nine and there can be 1 or two of them (again)

.

This looks for any single character (again)

([0-9]{4})

This looks for numbers zero through nine and there must be four of them in a row

So it is looking for a date in any of the following formats:

  • 04 18 1973
  • 04-18-1973
  • 04/18/1973
  • 04.18.1973

More will fit that pattern so it isn't a very good regex for what it is supposed to validate against. There are lots of sample regex patterns for matting dates in this format so if you google it you'll have a PCRE in no time.

ereg() to preg_match(), could you convert it for me?

Have a look at the preg_match(),

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

In your case $pattern = "/([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})/"
The $subject is the text which you want to match, which I assume is $dFecIni.
If you want to save the matches you provide the name of the variable where they are to be saved, $aFecIni for example,

The full function then become,

preg_match("/([0-9]{1,2})([0-9]{1,2})([0-9]{2,4})/", $dFecIni, $aFecIni); 

PHP - Convert ereg to preg_match

At a first glance, everything looks OK, but you need to make the regex case-insensitive by adding an i after the closing delimiter:

$pcexp[4] = '/^(bfpo)([0-9]{1,4})$/i';

and so on.

Your regexes don't allow for spaces between the parts of the postcode. Is this intentional? If not, add a \s* everywhere spaces are legal

Also, you can drop all instances of {1} and replace each {0,1} with ?.

Convert eregi to preg_match and ereg_replace to preg_replace

For preg_* functions you need add only delimiters for example / or # or other, in start and end of the pattern.

Write this

if ( (!preg_match('/^[a-z\d-]+\.([a-z]{2,4})$/i', $domain)) && (!preg_match('/^[a-z\d-]+\.([a-z]{2,4})+\.([a-z]{2,4})$/', $domain)))
echo $arrErrors['domi'] = 'Domain name appears to be invalid.';

function makeClickableLinks($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = preg_replace('#(((f|ht){1}tp://)[-a-z\d@:%_\+.~\#?&//=]+)#',
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = preg_replace('#(((f|ht){1}tps://)[-a-z\d@:%_\+.~\#?&//=]+)#',
'<a href="\\1" target=_blank>\\1</a>', $text);
$text = preg_replace('#([[:space:]()[{}])(www.[-a-z\d@:%_\+.~\#?&//=]+)#',
'\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
$text = preg_replace('#([_\.\da-z-]+@([\da-z][\da-z-]+\.)+[a-z]{2,3})#',
'<a href="mailto:\\1" target=_blank>\\1</a>', $text);
return $text;
}

Eregi to preg_match conversion

eregi('some expression', $input, $matches);

dark magic, chanting, spooky things...

preg_match('/some expression/i', $input, $matches);

You might want preg_match_all() instead, though.

PHP's eregi to preg_match conversion

You need to add the delimeters. I choose to use /, others choose ~ or #.

if ((preg_match('/opera mini/', $_SERVER['HTTP_USER_AGENT'])) == 1) 

Replace ereg_match with preg_match

Try this:

return ereg_replace("^(.*)%%number%%(.*)$","\\1$i\\2",$number);

becomes

return preg_replace("/^(.*)%%number%%(.*)$/","\\1$i\\2",$number);

Note the / around the regex.



Related Topics



Leave a reply



Submit