WebToTell

Full Version: PHP find all occurrences in string - PHP get string between two strings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.