regex - php split string on first occurrence of a letter -
i have following string
$string = "18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09 dr record + 2 21.47 20.24 27.15 20.24 record + 2 24.05 22.68"
i split @ first letter found (the letter varies each time). found similar question had following example
$split = explode('-', 'orange-yellow-red',2); echo $split[1]; //output yellow-red
but assuming know letter is. there way specify letter? i've used preg split before can't limit , i'm not regex.
if explode work regex may work, example not work.
$string = "18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09 dr record + 2 21.47 20.24 27.15 20.24"; $split = explode([a-za-z], $string,2);
use preg_split
$string = "18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09 dr record + 2 21.47 20.24 27.15 20.24 record + 2 24.05 22.68" $stringarray = preg_split("/[a-za-z]/",$string,2);
the [a-za-z] tells find first letter, , split there
$stringarray
hold 2 resulting strings, note takes out letter split on
you though use preg_match 2 strings not strip out first letter
preg_match("/([^a-za-z]+)(.*)/",$string,$matches);
here regular expression tells capture first letter, , capture after that
$matches[1]
contain
18/05-01/06 01/06-06/07 06/07-22/08 22/08-14/09
and $matches[2]
contain
dr record + 2 21.47 20.24 27.15 20.24 record + 2 24.05 22.68
$match[0]
holds whole string matched.
Comments
Post a Comment