views:

53

answers:

4

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.

+4  A: 

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#'
Mark Byers
A: 
<?
$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?

JapanPro
if someone could add why , someone vote down, so i can learn out of it?
JapanPro
I'm not the downvoter, your solution would work (although str_replace is probably more efficient). I'd never, ever post something with `short_open_tags` though, maybe that was it.
Wrikken
Two reasons: 1. your suggested alternative does not behave in the same way as the original, 2. no hint of a "better" solution or any real help at all with the OP's problem.
salathe
@Wrikken , post asked to "preg_replace" so there is no point in using str_replace. though thats good alternative.
JapanPro
@salathe as post ask convert to "preg_replace" so i have to stick with it, better is '/c\/o/i' "/i" which is an alternative to other solution. add some alternative if you have , so i can learn as well.
JapanPro
There's no need to suggest a "better" alternative if that "better" alternative does not do what the OP wants. The case-insensitivity modifier is completely superfluous. I don't think you need to learn anything, except perhaps actually answering the questions posed.
salathe
+3  A: 

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);
Gumbo
+1 Agreed. in this particular example, str_replace() is the function to use; there's no need for a regex at all. That said, we still need to solve the question for the other ereg to preg conversion he stated that he's doing, so str_replace() is really only a partial solution.
Spudley
bcmcfc
+2  A: 

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.

Spudley
Thanks for the in depth answer! There are a few uses of `[:space:]` in the regexes in the postcode function, which it would appear I should convert to `\s`?
bcmcfc
[:space:] can be converted to ... uh... a space character. hehe. Easy, eh? See also the reference links I've added to my main answer.
Spudley
D'oh! I actually have a (coffee stained) printout of that cheat sheet sat next to me, but without a proper understanding... it's just cheating! ;-)
bcmcfc