Forum Moderators: coopster

Message Too Old, No Replies

Help with regex in s&r array

         

Joe Belmaati

9:58 am on Nov 8, 2004 (gmt 0)

10+ Year Member



I am trying to strip the year from a string, so I have created an array like so:

<?php
// strip year
$patterns = array ("/\b[0-9]{4}\b/");
$replace = array("");
$description = preg_replace($patterns, $replace, $description);
?>

..but for reason it doesn't work. When I remove the count {4} the regex engine strips all numbers just fine, but my intention is to strip a literal of four consecutive numbers as in a year. (Nvm that this regex will also match 9823 or 0325 - for my site the simple solution will work).

Can anyone tell me what I am doing wrong? I have also tried:


array ("/\b\d{4}\b/");

but that doesn't work either.

Any helps is MUCH appreciated.

Sincerely,
Joe Belmaati
Copenhagen Denmark

coopster

5:41 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What does your data look like before you try to replace it?

Joe Belmaati

5:56 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Example:

1996 Mouton Rothschild Pauillac Bordeaux France

coopster

6:03 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ah, an excellent choice, Monsieur.
$subject = '1996 Mouton Rothschild Pauillac Bordeaux France'; 
$pattern = '/\d{4}/';
print preg_replace($pattern, '', $subject);

coopster

6:06 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And actually, your pattern will work, you just need to get the *array* stuff out of there. Your pattern may actually be more of what you want anyway, as you have added the word boundaries to it.

Joe Belmaati

6:13 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Ah, thank you thank you. As I am dealing with dynamic data that is pulled from a db I am sort of having to guess what'll come at me in $description - so I guess I have to put it into an array an re-assemble with preg_replace, no? (The string formatting is 100% strict. Year first, then expensive wine name) :D

coopster

6:18 pm on Nov 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, the example I showed there is using a string. You can assign the value returned by preg_replace to a string rather than just print it out again though.

Joe Belmaati

8:11 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Thank you very much, coopster!
Sincerely,
Joe

Joe Belmaati

9:45 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Now here is something bizarre:

<?php
// strip numbers (vintage)
$pattern = '/\d{4,4}/';
$description = preg_replace($pattern, '', $description);
?>

works perfectly, but

<?php
// strip numbers (vintage)
$pattern = '/\d{4}/';
$description = preg_replace($pattern, '', $description);
?>

gives me a parse error. I should state that my code is inside a phpbb bulletin board and that it is pulling topics out of a mysql db. Anyhow, I have solved my problem with coopster's help - just thought that the above mentioned is slightly weird.