Reputation: 371
what I'm trying to do is, given a string such as
trying to download some music, give me the details to download the music
get a matching result through preg_match_all that would give me something like
[0][0] => download some music
[0][1] => some
[1][0] => download the music
[1][1] => the
but I'm not able to achieve this. I'm using this pattern
§download(.*)music(.*)§
but the problem is that it just matches the first download
(the one from download some music
) and the last music
(the one from download the music
), while my goal is to find all occurrences and get the chars in between the two words (it should actually work for any number of words, but let's start with two).
Any help is appreciated, thanks! :)
UPDATE: After having the issue solved, I wrote an article on how this kind of regex can be used to make a gentle pattern match (or, rather, to compute pattern relevance in a given text): http://www.thecrowned.org/method-for-pattern-relevance-in-text
Upvotes: 0
Views: 191
Reputation: 67968
\bdownload\b(.*?)\bmusic\b
^^
Do a non greedy
search.See demo.
https://regex101.com/r/fM9lY3/29
$re = "/\\bdownload\\b(.*?)\\bmusic\\b/";
$str = "trying to download some music, give me the details to download the music";
preg_match_all($re, $str, $matches);
Upvotes: 3