Jump to content

Help Needed With preg_match_all()


shiny_spoon

Recommended Posts

Hey all,

 

I've always been a complete imbecile when it comes to preg functions. No matter how many tutorials I read or how many times I actually use it for simple searches, I never seem to get the hang of it. I'm at my wits end right now, so I figured I'd come on here and ask you all for some help! :P

 

I'm parsing HTML files like so:

$page = "http://www.location.com";
$page_contents = file_get_contents($page);
preg_match_all("I have no idea what to put here! ", $page_contents, $result);

 

I need to find the following pattern:

<div class="className">(pattern goes here)</div>

 

So basically anything within a certain div class. I've tried about two hundred possibilities and have gotten absolute nothing out of it! Hopefully one of you wise people can give me a hand! :)

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/53679-help-needed-with-preg_match_all/
Share on other sites

I have yet to get used to using preg_match, but here's one method that I have used in the past (Note: this is with consideration that you want only one value and the specific div we are looking for is unique to the rest of the code):

<?php

$text = file_get_contents($page);
$StartAt = "<div class\"class_name\">";
$EndAt = "</div>";

$startPos= stripos($text,$StartAt)+strlen($StartAt);
$endPos= stripos($text,$EndAt,$startPos)-$startPos;
$FinalVal=substr($text,$startPos,$endPos);

print $FinalVal;
?>

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.