Posted By

dmertl on 10/03/12


Tagged

substr String between substring


Versions (?)

Substring between two strings


 / Published in: PHP
 

Get the substring between two strings

  1. /**
  2.  * Returns the substring between two strings, delimiters not included
  3.  * @param string $string Haystack
  4.  * @param string $start Starting delimiter
  5.  * @param string|null $end Ending delimiter, if omitted will return the rest of the string
  6.  * @return bool|string The substring between $start and $end or false if either string is not found
  7.  */
  8. function substr_between($string, $start, $end=null) {
  9. if(($start_pos = strpos($string, $start)) !== false) {
  10. if($end) {
  11. if(($end_pos = strpos($string, $end, $start_pos + strlen($start))) !== false) {
  12. return substr($string, $start_pos + strlen($start), $end_pos - ($start_pos + strlen($start)));
  13. }
  14. } else {
  15. return substr($string, $start_pos);
  16. }
  17. }
  18. return false;
  19. }

Report this snippet  

You need to login to post a comment.