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.
if someone could add why , someone vote down, so i can learn out of it?
If you just have a fixed string that is to be replaced, why don’t you use simple
str_replace
?You shouldn't have replaced them. You probably need both:
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.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:
Both of those are in my permanent bookmarks.