Page 1 of 1

split string that start with .. and end with ..

Posted: Thu Jun 12, 2008 2:31 pm
by yacahuma
Need to split a string that contains something like

<b>10-10-2005 Radio 1</b>TEXTHERETEXTHERETEXTHERETEXTHERETEXTHERETEXTHERE<b>10-12-2005 Radio 1</b>TEXTHERETEXTHERETEXTHERETEXTHERETEXTHERETEXTHERE

What I want at the end is an array like
[1]10-10-2005
[2]10-12-2005

Thank you

Re: split string that start with .. and end with ..

Posted: Thu Jun 12, 2008 2:39 pm
by prometheuzz

Code: Select all

<?php
if(preg_match_all(
    '/\d?\d-\d?\d-\d{4}/', 
    '<b>10-10-2005 Radio 1</b>TEXTHERETEXTHERETEXTHERETEXTHERETEXTHERETEXTHERE
     <b>10-12-2005 Radio 1</b>TEXTHERETEXTHERETEXTHERETEXTHERETEXTHERETEXTHERE',
    $matches)) {
  print_r($matches);
}
?>

Re: split string that start with .. and end with ..

Posted: Thu Jun 12, 2008 4:12 pm
by GeertDD
Prometheuzz's regex will do the job. However, what if the "textheretexthere" part contained a date as well? It would get matched too. I suggest to slightly alert the regex to provide an extra hook to the preceding <b>.

Code: Select all

Before:          \d?\d-\d?\d-\d{4}
After:   (?<=<b>)\d?\d-\d?\d-\d{4}

Re: split string that start with .. and end with ..

Posted: Fri Jun 13, 2008 5:39 am
by yacahuma
Thank you all. This works.