KapaGino Posted December 29, 2013 Share Posted December 29, 2013 Hi, I'm trying to grab a particular portion of text from a websites source code using preg_match but I'm not getting a result. <?php $url = "http://www.estatesgazette.com/propertylink/advert/4th_floor_pear_mill_industrial_estate_stockport_cheshire-stockport_cheshire-3383230.htm"; $html = htmlspecialchars(file_get_contents($url)); preg_match('%image-carousel">.+?jpg%i', $html, $match); echo $match[0]; ?> I've tried the pattern in RegExr and it did exactly what I wanted. I'm running PHP 5.4.7 btw. Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/284963-trouble-with-preg_match/ Share on other sites More sharing options...
trq Posted December 30, 2013 Share Posted December 30, 2013 You should probably tell us exactly what it is your trying to match. Link to comment https://forums.phpfreaks.com/topic/284963-trouble-with-preg_match/#findComment-1463238 Share on other sites More sharing options...
objnoob Posted December 30, 2013 Share Posted December 30, 2013 preg_match()'s 3 parameter is passed by reference. you need to make sure you declare it so the function can refer to it. $match = array(); # declared, initialized, and referable preg_match('%image-carousel">.+?jpg%i', $html, $match); echo $match[0]; Edit: Nevermind, I guess you don't have to. I forget how easy PHP really is :] Link to comment https://forums.phpfreaks.com/topic/284963-trouble-with-preg_match/#findComment-1463239 Share on other sites More sharing options...
Psycho Posted December 30, 2013 Share Posted December 30, 2013 Why are you running htmlspecialchars() on the content before you extract the content you want? According to your regex youa re looking for content that comes after a tag with a parameter ending in 'image-carousel">'. The htmlspecialchars() will convert that closing '>' into '<' and your regex will not match it. Link to comment https://forums.phpfreaks.com/topic/284963-trouble-with-preg_match/#findComment-1463240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.