WebToTell
PHP find all occurrences in string - PHP get string between two strings - Printable Version

+- WebToTell (https://www.webtotell.com)
+-- Forum: PHP for beginners and advance users.PHP Code samples, applications. (https://www.webtotell.com/Forum-PHP-for-beginners-and-advance-users-PHP-Code-samples-applications)
+--- Forum: PHP (https://www.webtotell.com/Forum-PHP)
+---- Forum: PHP Functions (https://www.webtotell.com/Forum-PHP-Functions)
+---- Thread: PHP find all occurrences in string - PHP get string between two strings (/Thread-PHP-find-all-occurrences-in-string-PHP-get-string-between-two-strings)



PHP find all occurrences in string - PHP get string between two strings - turgam - 03-28-2019

This PHP function finds all occurrences in a string and assign them to an array, 
 which gives you both "find all occurrences in string"  and  "get string between two strings" 
PHP Code:
function get_every_string_between($str$start$end){
  $contents = array();
  $startLength strlen($start);
  $endLength strlen($end);
  $startX $strStart $contentEnd 0;
  while (false !== ($strStart strpos($str$start$startX))) {
    $strStart += $startLength;
    $contentEnd strpos($str$end$strStart);
    if (false === $contentEnd) {
      break;
    }
    $contents[] = substr($str$strStart$contentEnd $strStart);
    $startX $contentEnd $endLength;
  

and usage:
$zipFiles = get_every_string_between($page, '<a class="download_link" href="','">') ;
echo "<pre>" ;
print_r( $zipFiles );
This examples allow me to find all zip file links in a web page but there are many other ways to using it.