Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP find all occurrences in string - PHP get string between two strings
#1

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)