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. Quote 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. Quote 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 (edited) 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 :] Edited December 30, 2013 by objnoob Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/284963-trouble-with-preg_match/#findComment-1463240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.