Jump to content

Text Parsing


ilikemath2002

Recommended Posts

Can I use cURL instead of file_get_contents? It would look something like this:

<?php
SOMECURLCODE
$data = curl_exec($ch);
$regex = '/Page 1 of (.+?) results/';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
?>

 

Also, how do I change this source to be used as $regex:

<span class="billing funorb">
<span class="credits"><b>(.+?)</b><br />Days</span>

Link to comment
https://forums.phpfreaks.com/topic/144470-text-parsing/
Share on other sites

Can I use cURL instead of file_get_contents? It would look something like this:

 

Yes you can use curl, but it is meant for real interaction between two servers.  Like, if you want to send data to a server's form and get the data generated from the response.  That sort of thing.  If you're just scraping the page, curl is making it more complicated than it needs to be.  Stick with file_get_contents.

 

Also, how do I change this source to be used as $regex:

<span class="billing funorb">
<span class="credits"><b>(.+?)</b><br />Days</span>

 

Don't quite understand what you mean...are you saying you want to grab the stuff between the bold tags?

 

// may or may not need the 's' modifier, depending on if boundaries will be on multiple lines or not
$regex = '~<span class="credits"><b>(.+?)</b>~s';
preg_match($regex,$data,$match);

 

Link to comment
https://forums.phpfreaks.com/topic/144470-text-parsing/#findComment-758261
Share on other sites

Thanks CV, that's what I needed, I thought it would need \ or something before the tags.

 

Just depends on what delimiter you are using for the pattern.  For instance, if I chose / as my pattern delimiter, I would have to escape any /'s in the pattern, like this:

 

"/<span class="credits"><b>(.+?)<\/b>/s"

so the regex engine knows that that / for the closing b tag isn't the end of the pattern.

Link to comment
https://forums.phpfreaks.com/topic/144470-text-parsing/#findComment-758505
Share on other sites

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.