Search code examples
phpcodeignitersubstr

how to truncate the string, or to extract a portion of the value from a string in php


hello , i have value 1284&kec=220107&prop=220000

here i want to take the numbers in front & the numbers in front are not always three letters, sometimes two letters and sometimes also one, how to remove or delete values ​​after &

example : 1284&kec=220107&prop=220000
result value = 1284

example : 11&kec=2332&prop=563454
result value = 11

can you give an example?


Solution

  • You can use explode() to convert the string to an array and then get what you want. Like so -

    $example = '1284&kec=220107&prop=220000';
    $ex = explode("&kec", $example); // convert it to array where $kec is found
    $value = $ex[0]; // output: 1284 // the first key will hold the value before $kec
    
    $example2 ='11&kec=2332&prop=563454';
    $ex = explode("&kec", $example2);
    $value = $ex[0]; // output: 11