www.webdeveloper.com
Results 1 to 2 of 2

Thread: Finding matched keywords in a exploded string.

  1. #1
    Join Date
    May 2007
    Posts
    104

    Finding matched keywords in a exploded string.

    Hello,

    I have a large chunk of data that i'm using the below code to explode using an array of keywords, basically the data i have is sections which i need to split up, but there are potentially more than 3 sections in some cases.

    PHP Code:
    $string "start time 00:30 mid time 00:45 end time 01:00"
    $delim = array('start''mid''end');
    $step_01 str_replace($delim'+delim+'$string); //Extra step to create a uniform value
    $exploded_string explode('+delim+'$step_01);

    print_r($exploded_string); 
    //prints out
    //[0]=> time 00:30 [1]=> time 00:45 [2]=> time 01:00 
    If there is four sections, it will print out this
    //[0]=> time 00:30 [1]=> time 00:45 [2]=> time 01:00 [3]=> time 02:00

    What i need to know, is there a way of finding out the matched keyword from the array to each section, e.g [2] = "End"

    Or possibly when a match is found, making a new array matching keywords to array parts?

    I'm a bit stuck on this! any help is much appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,916
    I might use preg_match_all(), e.g. (untested):
    PHP Code:
    preg_match_all('#(\S+) time (\d+:\d+)#i'$string$matchesPREG_SET_ORDER);
    echo 
    "<pre>".print_r($matchestrue)."</pre>"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles