PHP - Getting a sub-string from a string

前端 未结 4 1701
野的像风
野的像风 2021-01-15 19:17

I have a string as

$line = \"Name=johnGender=M\";

How to make a string called $name that will have a value stored as joh

4条回答
  •  忘掉有多难
    2021-01-15 20:01

    Try this :

    function strinbetween($inputstring, $start, $end){
        $inputstring = " ".$inputstring;
        $ini = strpos($inputstring,$start);
        if ($ini == 0) {
            return "";
        }
        $ini += strlen($start);
        $len = strpos($inputstring,$end,$ini) - $ini;
        return substr($inputstring,$ini,$len);
    }
    

    Call the above function and pass the string, starting string and ending string

    $line = "Name=johnGender=M";
    $parsed = strinbetween($line,'=','G');
    echo $parsed;
    

    So here it will return john

提交回复
热议问题