Jump to content

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

Edited by objnoob

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.