Jump to content

Trouble with Preg_Match


KapaGino

Recommended Posts

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. :confused:

 

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

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 :]

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.