ereg_replace to preg_replace for a particular rege

2019-02-26 21:33发布

问题:

I've converted some eregs to preg_matches by replacing the initial ^ and final $ with /s (I hope that was sufficient), but I have an ereg_replace in the same function that I'm not sure about.

It's from a checkPostcode function to be found here.

// Take account of the special BFPO c/o format
$postcode = ereg_replace ('C\/O', 'c/o ', $postcode);

If I change it to preg_replace without altering the regex it gives this error: Delimiter must not be alphanumeric or backslash

Not really having a solid grasp of regex I'm not sure what I need to change whilst ensuring the pattern still does its job.

回答1:

Firstly, as mentioned elsewhere, you should probably use str_replace() rather than regex for simple cases like the one in your example.

Now, a quick explaination about regex in PHP:

As you clearly already know, the ereg_* functions are now deprecated in PHP, and you should use the preg_* functions instead.

The main difference between the two sets of functions is the type of regular expression that they use. The ereg functions use POSIX-style regex, while the preg functions use PERL-style regex. The PHP developers have decided to standardise on PERL-style regex, hence why the ereg functions are being deprecated.

I mention all this because it is helpful to understand the differences between the two types of regex when converting from one to the other.

For the most part, they are actually very similar - most common regex codes work the same between them both. For example, the ^ and $ are the same in both, denoting anchors to the begining and end of the string.

Where they differ in a big way is that the preg functions do not support the wordy character classes that ereg supports. So if you have any eregs that include codes like [:digit:] or [:alpha:] then you'll have more work to convert them. They can still be converted, though.

There are a few other differences between the two, but unless you're doing complex expressions (and it doesn't sound like it), you're unlikely to run into any other problems converting between the two styles.

Other than that, the one big difference is that preg requires a delimiter character at either end of the regex string to denote that it's a regex. (this is usually a slash, but can be almost any symbol as long as it's the same at both ends). This is not the same as the ^ and $, though, so your remark about replacing them is incorrect; you still need the ^ and $ as well as the slashes.

Therefore: to convert ereg code like this: '^xyz$', you would just add slashes, like so: '/^xyz$/'

Hope that helps.

[edit]

Just a quick edit to give you some additional reference material:

  • http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
  • http://www.regular-expressions.info/reference.html

Both of those are in my permanent bookmarks.



回答2:

I've converted some eregs to preg_matches by replacing the initial ^ and final $ with /s

You shouldn't have replaced them. You probably need both:

'/^.....$/'

Not really having a solid grasp of regex I'm not sure what I need to change C\/O whilst ensuring the pattern still does its job.

Normally you use / to delimit the regular expression, but in this case it would be a good idea to choose some other non-alphanumeric character so that you don't have to escape the slash.

'#C/O#'


回答3:

If you just have a fixed string that is to be replaced, why don’t you use simple str_replace?

$postcode = str_replace('C/O', 'c/o ', $postcode);


回答4:

<?
$postcode = "test C/O";
echo ereg_replace ('C\/O', 'c/o ', $postcode);
//test c/o
echo preg_replace ('/c\/o/i', 'c/o ', $postcode);
//test c/o
?>

if someone could add why , someone vote down, so i can learn out of it?