PHP - Getting a sub-string from a string

前端 未结 4 1645
野的像风
野的像风 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 19:46

    I guess the following would work for you.

    $name = array();
    foreach($line as $elem)
        array_push($name,substr(explode("=",$elem)[1],0,4));
    

    So you can access it as $name[0],$name[1],...

    Better try to have the input syntax as

    $line = array("Name=john&Gender=M","Name=raja&Gender=M");
    parse_str($line);
    

    So you can use the built-in function parse_str().

    Now $Name[0] contains john and $Name[1] contains raja.

提交回复
热议问题