Extract a substring between two characters in a string PHP

前端 未结 10 1484
逝去的感伤
逝去的感伤 2020-12-02 22:38

Is there a PHP function that can extract a phrase between 2 different characters in a string? Something like substr();

Example:

$String          


        
10条回答
  •  爱一瞬间的悲伤
    2020-12-02 23:09

    (moved from comment because formating is easier here)

    might be a lazy approach, but in such cases i usually would first explode my string like this:

    $string_array = explode("=",$String); 
    

    and in a second step i would get rid of that "]" maybe through rtrim:

    $id = rtrim($string_array[1], "]");
    

    ...but this will only work if the structure is always exactly the same...

    -cheers-

    ps: shouldn't it actually be $String = "[modid=256]"; ?

提交回复
热议问题