SKLAK
SKLAK

Reputation: 3965

How to find all substrings between two delimiters in php?

For example, assume there is a string

"var name=cat; *bunch of random strings* var name=dog; *bunch of random strings* var name=cow; *bunch of random strings*"

I want to parse this string and find all the strings between the two delimiters, "name=" and ";". So, I want to extract the words cat, dog, and cow and put them in an array. Any idea how I should do this?

Upvotes: 1

Views: 1453

Answers (1)

jameslafferty
jameslafferty

Reputation: 2182

I don't know that explode does the trick, but this seems to:

$string = "var name=cat; *bunch of random strings* var name=dog;*bunch of random strings*var name=cow;*bunch of random strings*";
preg_match_all('/name=([^;]+)/', $string, $matches);
print_r($matches[1]);

or, as a function :

function wizbangStringParser ($string) {
    preg_match_all('/name=([^;]+)/', $string, $matches);
    return $matches[1];
}

Upvotes: 4

Related Questions