/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]
$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
[/code]

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.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 11.2012 — I might use preg_match_all(), e.g. (untested):
[code=php]
preg_match_all('#(S+) time (d+:d+)#i', $string, $matches, PREG_SET_ORDER);
echo "<pre>".print_r($matches, true)."</pre>";
[/code]
×

Success!

Help @copes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...