phpregextext-extraction

Get text that follows a regex pattern match


I'm struggling to find out how I can get the text after a regex match (until the end of word).

Ex 1:
Input: my home
Regex: /hom/
Result: e

Ex 2:
Input: soulful
Regex: /soul/
Result: ful


Solution

  • You can use:

    $str = 'soulful';
    $reg = '/soul(\w+)/';
    if ( preg_match($reg, $str, $m) )
       print_r ($m);
    

    OUTPUT

    Array
    (
        [0] => soulful
        [1] => ful
    )