note 103994 deleted from function.preg-match by cmb

From: Date: Sun, 28 Oct 2018 10:16:57 +0000
Subject: note 103994 deleted from function.preg-match by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209535@lists.php.net to get a copy of this message
Note Submitter: mulllhausen ---- i do a fair bit of html scraping in conjunction with curl. i always need to know if i have reached the right page or if the curl request failed. the main problem i have encountered is html tags having unexpected spaces or other characters (especially the &nbsp; character sequence) between them. for example when requesting a page with a certain manner set of post or get variables the response might be <a href='blah'><span>data data data</span></a> but requesting the same page with different post/get variables might give the following result: <a href='blah'> &nbsp;<span>data data data</span> </a> to match both of these tag sequences with the same pattern i use the [\S\s]*? wildcard which basically means 'match anything at all...but not if you can help it' so the pattern for the above sequence would be: <?php $page1 = "........<a href='blah'><span>data data data</span></a>........."; $page2 = "........<a href='blah'> &nbsp;<span>data data data</span> </a> ........"; $w = "[\s\S]*?"; //ungreedy wildcard $pattern = "/\<a href='blah'\>$w\<span\>data data data\<\/span\>$w\<\/a\>/"; if(preg_match($pattern, $page1, $matches)) echo "got to page 1. match: [".print_r($matches, true)."]\n"; else echo "did not get to page 1\n"; if(preg_match($pattern, $page2, $matches)) echo "got to page 2. match: [".print_r($matches, true)."]\n"; else echo "did not get to page 2\n"; ?>

« previous php.notes (#209535) next »