phpregexfind

regex-find values with specific pattern


I want to find specific values, from an string.

I have a string like:

$str = '
[mytips id="101" zcode="Volvo"]
[mytips id="321" zcode="BMW"]
[mytips id="423" zcode="Toyota"]
';

I need to get all the values of attribute zcode. So I can get a list/array like:

Array(
[0] => Volvo
[1] => BMW
[2] => Toyota

Thanks in advance


Solution

  • You can use regex in preg_match_all() at php.

    preg_match_all('/zcode\s*=\s*"([^"]*)"/', $str, $m);
    print_r($m[1]);