user2076160 user2076160 - 1 year ago 100
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'

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
my other sentence

I looked into the use of
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 Source
preg_match("/((\babc\b)|(\bdef\b)) '[a-zA-Z]+: (.*?)'/", $your_string, $matches);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download