phptext-extraction

Extract a value from a word then an equals sign until a semicolon


I have a string value from which I need to extract the value1, is there a PHP function which can be tweaked and used, in order to cover edge cases

 "value1=php; value2=.php.net; Expires=Nov, 12-May-2020 20:51:23 GMT; Path=/; random;"

How to do it?


Solution

  • You can use preg_match and the regex /value1=(.*?);/ to capture the content.

    $s = "value1=php; value2=.php.net; Expires=Nov, 12-May-2020 20:51:23 GMT Path=/; random;";
    
    preg_match("/value1=(.*?);/", $s, $matches);
    print_r($matches[1]); => php