note 107013 deleted from function.substr by danbrown
| From: | danbrown@php.net | Date: | Tue, 03 Jan 2012 21:56:32 +0000 |
| Subject: | note 107013 deleted from function.substr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185304@lists.php.net to get a copy of this message | ||
Note Submitter: divinity76 at gmail dot com
----
i use this function to search html files
function myHTMLSearchFunction($haystack,/*array*/$needleArray,/*string*/$subStrThis,/*start pos
int*/$offset=0,&$lastoffset=0)
{
$newOffset=0;//just initialize it...
if($offset>0)
{
$haystack=substr($haystack,$offset);
}
//$lastoffset+=$offset;
if($haystack===false)
{return false;}
foreach($needleArray as $stuff)
{
$newOffset=strpos($haystack,$stuff)+strlen($stuff);
if($newOffset===false)
{return false;}
$lastoffset+=$newOffset;
$haystack=substr($haystack,$newOffset);
if($haystack===false)
{return false;}
//echo "<br/><br/>Stuff:
".htmlentities($stuff)."<br/><br/><br/>";
//echo "<br/><br/>New haystack:
".htmlentities($haystack)."<br/><br/><br/>";
}
$subStrThis=strpos($haystack,$subStrThis);
if($subStrThis===false)
{return false;}
//echo "Haystack is now:".htmlentities($haystack);die();
//$lastoffset+=$subStrThis;
$result=substr($haystack,0,$subStrThis);
return $result;//wich, if failed, is ===false. or so i hope. :p
}
Example:
$html=file_get_contents("http://www.google.com");
echo 'googles logo image location:
www.google.com',myHTMLSearchFunction($html,array('<div','id=lga','>','background:url('),')',0);
//example output: googles logo image location: www.google.com/intl/en_com/images/srpr/logo1w.png