preg_split() and Regex - what I wrote doesn't show original text

Discussion in 'Programming' started by tlshaheen, Oct 18, 2010.

  1. #1
    I have the following code in a while loop - the strings its parsing look like "Event 10 Men's 800 Yard Run". I want it to split the string after the event number, and have the event name below it. So like:
    Event 10
    Men's 800 yard run

    The problem is, $events[0] does not hold any information - $events[1] holds Men's 800 Yard Run, but I cannot get it to store Event 10. Help please :)

    $events = preg_split("/Event\x20[0-9]{2}/", $element, PREG_SPLIT_DELIM_CAPTURE);
    echo $events[0]." <br /> ".$events[1]." <br />";
    PHP:
    print_r($events) yields
    Array ( [0] =>  [1] => Men's 800 Yard Run )
    Code (markup):
     
    Last edited: Oct 18, 2010
    tlshaheen, Oct 18, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    With preg_split, the regular expression defines the delimter or "needle". Since there is nothing preceeding the delimeter in your "haystack", the first array element is empty. I think preg_match() would be a better choice for you.
     
    rainborick, Oct 18, 2010 IP