user2076160 user2076160 - 1 month ago 7
PHP Question

PHP use regex to return substring

I want to retrieve a substring out of a other string.

The beginning of the string has 2 possibilities. Let's say 'abc' or 'def'
After this string there is a ' character and a random word. After the word there is a colon and then a sentence. And at least the string ends with a ' again.

So some possibilities are:

abc 'randomword: my sentence'

or
def 'otherrandomword: my other sentence'


What I want to do is with PHP retrieve the sentence in the string.
So for the samples above there should be
my sentence
and
my other sentence
.

I looked into the use of
preg_replace
and
preg_match
but I can't find a solution for this problem.

Could someone help me out with this. I hope that I made everything clear. If any questions please ask.

sg- sg-
Answer
preg_match("/((\babc\b)|(\bdef\b)) '[a-zA-Z]+: (.*?)'/", $your_string, $matches);
var_dump($matches[4]);