PHP - Getting a sub-string from a string

前端 未结 4 1694
野的像风
野的像风 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:08

    Suppose we dont know the length of the name. Say, $line = "Name=SaifUrRehmanGender=M";

    Use strpos() to get the index of "Gender"

    The strpos() function finds the position of the first occurrence of a string inside another string.

    For your case: $name = substr($line,5,strpos($line,"Gender")-5); will do :)

    Output: SaifUrRehman

提交回复
热议问题