TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Need Help with RegEx (http://www.talkphp.com/absolute-beginners/1540-need-help-regex.html)

webosb 11-28-2007 10:43 PM

Need Help with RegEx
 
I'm trying to code something that parses content the following content:

PHP Code:

<!-- Begin cached content "main" updated on Mon Nov 26 12:00:01 EST 2007 -->
<
div id="maincontent">
<
h2>Resources</h2>
<
ul>
GRAB THIS CONTENT HERE
</ul>
</
div>
<
hr id="seperator"/>
<!-- 
End cached content "main" --> 

I'm using this code:

PHP Code:

<?php
$url 
"http://www.somewebsite.com"
$data file_get_contents($url);
$regex '<!-- Begin cached content (.+?) <!-- End cached content "main" -->';
preg_match($regex,$data,$match);
var_dump($match); 
echo 
$match[1];
?>

Can someone help me fix the regex part so it will grab all the content above?

Wildhoney 11-28-2007 11:01 PM

Something like this, perhaps?

php Code:
preg_match('/(<!-- Begin cached content .* content "main" -->)/iUs', $szContents, $aMatches);

You can then find it in $aMatches[1].

webosb 11-29-2007 07:29 PM

that worked! thanks alot wildhoney!

*edit* is this example faster than using CURL to get the website content?

Wildhoney 11-29-2007 09:24 PM

I'd say so, yes. Although file_get_contents isn't as in-depth as cURL, for something like that, it's better to stick to the simplicity side than calling a group of functions that are somewhat overkill for what you require.

bluesaga 12-01-2007 12:38 AM

Generally speaking it will be dependent on the website that you are trying to open but generally speaking, while cURL may take a bit longer to implement its by FAR faster than file_get_contents in the long run....

Quote:

I have created a script that tests all 4 methods on 20 different websites 10 times each. The results are clear, here is one set of the results:

Curl time: 43.02 seconds
FGC time: 86.48 seconds
Fopen time: 86.34 seconds
Socket time: 44.91 seconds
Quoting: http://uk2.php.net/manual/en/ref.curl.php#75126

Wildhoney 12-01-2007 02:48 AM

If you were to have asked me before-hand about which I thought was faster, FGC or cURL, I would have been more inclined to have said FGC. I was under the false impression that it was a right little light-weight in comparison to cURL! Seems cURL is actually the master of both speed and functionality.

Geert 12-09-2007 04:17 PM

Quote:

Originally Posted by Wildhoney (Post 4725)
php Code:
preg_match('/(<!-- Begin cached content .* content "main" -->)/iUs', $szContents, $aMatches);

You can then find it in $aMatches[1].

Small tip. You can speed up the regex by removing the capturing parentheses. You don't need them because the full pattern match is already included in $aMatches[0].


All times are GMT. The time now is 09:47 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0