|
-
May 23rd, 2004, 10:10 AM
#1
Thread Starter
Fanatic Member
Regular expression help needed [Resolved]
Hi all
I need to create a regular expression so that I can use the preg_split function to split up a table by its td elements
PHP Code:
$rows=preg_split("^(<tr)+(</tr>)$",$table);
This is the warning i get:
Warning: No ending delimiter '^' found in c:\inetpub\wwwroot\nkgroup\admin\parser.php on line 63
And the table DOES contain rows. I have searched on the net and I cant find an explanation.
If you do have one, I would be reaaalllllly grateful!!!
Thanks in advance
Last edited by MikkyThomeon; May 23rd, 2004 at 10:51 PM.
-
May 23rd, 2004, 10:21 AM
#2
Perl style regular expressions should be delimeted with solidus'. The following should work:
PHP Code:
$rows=preg_split("/^(<tr)+(</tr> )$/",$table);
-
May 23rd, 2004, 01:32 PM
#3
Thread Starter
Fanatic Member
Hi again
VisualAd, thanks - I tried your tip but it returned the following error:
Warning: Unknown modifier 't' in c:\inetpub\wwwroot\nkgroup\admin\parser.php on line 63
Why is php giving me this error? Again your help would be much appreciated.
Just for helping out, here is a freebie function for you that works quite well:
PHP Code:
function downloadtable($url,$tablesearchkey)
{
$start=false;
$end=false;
$line="";
$body="";
$file = fopen ($url, "r");
if (!$file)
{
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file))
{
$line= fgets ($file, 512);
if($start==false)
{
$pos=strpos($line,$tablesearchkey);
if($pos)
{
$line=substr($line,$pos-1,strlen($line)-$pos);
$body.=$line;
$start=true;
}
}
else
{
if($end==false)
{
$pos=strpos($line,"</table>");
if($pos)
{
$pos+=7; //ent table marker length
$line=substr($line,1,$pos);
$body.=$line;
$end=true;
}
else
{
$body.=$line;
}
}
}
}
fclose($file);
return($body);
}
-
May 23rd, 2004, 02:24 PM
#4
Sorry, I didn't look at the acutal regular expression. The / character in the expression must be escaped with a "\" otherwise it will think the first one is the end of the expression and cause an error.
PHP Code:
$rows=preg_split("/^(<tr)+(<\\/tr> )$/",$table);
What is it you are doing? If you are trying to extract all the text in between table rows I would recommend you use preg_match_all and not preg_split which splits a string which is dlimetered by a the regular expression.
Thanks for the function by the way. But what exactly does it do
-
May 23rd, 2004, 03:21 PM
#5
Thread Starter
Fanatic Member
The function basically extracts a table from a web page. If you specify the <table...> in the target page, then the complete table will be returned.
I am trying to use this combined with the regular expressions so that I can load the weather data into my own database and do custom data analysis, as well as create a wml fromt end for this information. Whenever I get the chance to surf, I will know which spot is the best to go to and all I have to do is check on my cell to save myself a lot of time and travelling.
Try using the function like this:
PHP Code:
$test = downloadtable("http://www.weathersa.co.za/wx/CurrentWx/Current.jsp?synopNo=68593","<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\">");
Of course, I need to parse the td tags into fields that can be stored in a database. I will try preg_match_all - sounds good...
-
May 23rd, 2004, 05:44 PM
#6
Thread Starter
Fanatic Member
PHP Code:
preg_match_all("|<td[^>]+>(.*)</[^>]+>|U",$table,$out,PREG_SET_ORDER);
After your tip off I looked in the php manual and found the way above which worked. For some reason I can still not get the original method to work.
Thanks for your help!!! Now I can get custom weather reports on my phone!!!
-
May 23rd, 2004, 10:51 PM
#7
Thread Starter
Fanatic Member
here is the code if anybody else would like to do sonething similar...
PHP Code:
<?
//this script takes an existing page on the internet and parses all the
//tags on the page into. An array. Then another function can be called
//to takes this array and extract the tables from the array. Each
//table will then be represented as a new 2d array
function downloadtable($url,$tablesearchkey)
{
$start=false;
$end=false;
$line="";
$body="";
$file = fopen ($url, "r");
if (!$file)
{
echo "<p>Unable to open remote file.\n";
exit;
}
while (!feof ($file))
{
$line= fgets ($file, 512);
if($start==false)
{
$pos=strpos($line,$tablesearchkey);
if($pos)
{
$line=substr($line,$pos-1,strlen($line)-$pos);
$body.=$line;
$start=true;
}
}
else
{
if($end==false)
{
$pos=strpos($line,"</table>");
if($pos)
{
$pos+=7; //ent table marker length
$line=substr($line,1,$pos);
$body.=$line;
$end=true;
}
else
{
$body.=$line;
}
}
}
}
fclose($file);
return($body);
}
function parsetable($table,$skiprows)
{
$rowcount=0;
$buffer="";
$cnt=0;
$ret="";
// send wml headers
header("Content-type: text/vnd.wap.wml");
echo "<?xml version=\"1.0\"?>";
echo "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""
. " \"http://www.wapforum.org/DTD/wml_1.1.xml\">";
?>
<wml>
<card id="card1" title="WeatherCheck"><p><?
preg_match_all("|<td[^>]+>(.*)</[^>]+>|U",$table,$out,PREG_SET_ORDER);
for($i=1;$i<14;$i+=2)
{
$ret.= trim($out[$i][1]).trim($out[$i+1][1])."<br/>"; //.$out[$i+1][1]."
}
$ret=str_replace("\n","",$ret);
print $ret;
print "</p></card></wml>";
}
$test = downloadtable("http://www.weathersa.co.za/wx/CurrentWx/Current.jsp?synopNo=68593",
"<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\">");
$replaced=str_replace(array("<b>","</b>","<strong>","\n","&"),"",$test);
parsetable($replaced,1);
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|