PHP - Return everything after delimiter

后端 未结 7 1697
北恋
北恋 2021-01-19 01:00

There are similar questions in SO, but I couldn\'t find any exactly like this. I need to remove everything up to (and including) a particular delimiter. For example, given t

7条回答
  •  渐次进展
    2021-01-19 01:20

    Sample String:

    $string = 'value:90|custom:hey I am custom message|subtitute:array';
    

    convert string to array

    $var = explode('|', $string);
    

    Check Results:

    Array(
    [0] => value:90
    [1] => custom:hey I am custom message
    [2] => subtitute:array)
    

    Declare an array variable

    $pipe = array();
    

    Loop through string array $var

    foreach( $var as $key => $value ) {
      // get position of colon
      $position = strrpos( $value, ':' );
      // get the key
      $key = substr( $value, 0, $position );
      //get the value
      $value = substr( $value, $position + 1 );
      $pipe[$key] = $value; }
    

    Final Result:

    Array(
    [value] => 90
    [custom] => hey I am custom message
    [subtitute] => array)
    

提交回复
热议问题