Jump to content

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;
?>

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.