phpsubstr

Extract a substring between two characters in a string PHP


Is there a PHP function that can extract a phrase between 2 different characters in a string? Something like substr();

Example:

$String = "[modid=256]";

$First = "=";
$Second = "]";

$id = substr($string, $First, $Second);

Thus $id would be 256

Any help would be appreciated :)


Solution

  • use this code

    $input = "[modid=256]";
    preg_match('~=(.*?)]~', $input, $output);
    echo $output[1]; // 256
    

    working example http://codepad.viper-7.com/0eD2ns