You are not logged in.

#1 2006-02-16 03:48:15

FJ
Member
From: Ontario, Canada
Registered: 2005-03-17
Posts: 75
Website

Help a PHP proto-newbie

I have a bash script that I want to convert into a PHP script so I can use it on a webpage.  Even though the script is very simple, I have no experience in PHP really so I'm confused how to go about this.  Here is an example of something I'm having problems with.  This is a bash snippit:

MyVariable=`grep -o 'text1>[0-9]+' foo.html 
| sed -e 's@^.*>@@g'`

All I'm doing there is grepping a text/html file for some specific content, and then filtering it to get just the integer substring.  I'm guessing I need to use the string replace function, but I'm not sure how to go about it.  Can someone help me out in converting this to PHP code?

Offline

#2 2006-02-17 20:43:30

Michiel
Member
From: Westkapelle, Zeeland, NL
Registered: 2005-02-13
Posts: 34
Website

Re: Help a PHP proto-newbie

Can you give an example of the whole string you want to get info from (the foo.html in this example)
I guess you can make a simple pattern for preg_match


All you got to do, is tell the story right...

Offline

#3 2006-02-18 00:28:06

FJ
Member
From: Ontario, Canada
Registered: 2005-03-17
Posts: 75
Website

Re: Help a PHP proto-newbie

foo.html is a webpage I scraped.  Here is a chunk of it, within which I want to search:

</td></tr></table><td class=darkbg width=50%><tr><td class=lightbg width=50%><td width=740 class=lightbg nowrap><table border=0 width=100% cellspacing=1 cellpadding=5><tr><td class=lightbg colspan=2><div id=stats><table border=0 width=100% cellpadding=0 cellspacing=6><td width=50% valign=top align=right class=lightbg>a 26 year-old single guy<br><b><a href="http://www.stumbleupon.com/city/hamilton/"><b>Hamilton</b></a>, <a href="http://www.stumbleupon.com/state/ontario/"><b>Ontario</b></a>, <a href="http://www.stumbleupon.com/country/canada/"><b>Canada</b></a></b><br><br><span class=mini>Data about me.</span><td align=center valign=top class=lightbg nowrap=true><a href="/about/"><img border=0 hspace=3 alt="FJ" src="http://www.stumbleupon.com/mediumpics/232345.jpg"></a><td width=50% valign=top class=lightbg>Active <span class=text1>Feb 17</span><span class=mini> · Joined Jan 2/05</span><br><img src="http://www.stumbleupon.com/images/thumbup.gif">'s: <span class=text1>517</span> · Fans: <span class=text1><b>44</b></span> <img src="http://www.stumbleupon.com/images/arank4.gif"></table>

The substring I want from the above is 517, to be turned into an integer.  Thanks for any guidance you can give me.

Offline

#4 2006-02-18 09:59:26

Michiel
Member
From: Westkapelle, Zeeland, NL
Registered: 2005-02-13
Posts: 34
Website

Re: Help a PHP proto-newbie

if there is only one match per string, you could use this:

<?php
    // Get the file contents
    $contents = file_get_contents('foo.html');

    // Get the matches
    preg_match('#text1>(d+)<#', $contents, $matches);

    print $matches[1]; // The match
?>

If there are more matches per string, you could use preg_match_all, the matches will be $matches[1][0], $matches[1][1] etc etc

Good luck


All you got to do, is tell the story right...

Offline

#5 2006-02-24 00:26:57

FJ
Member
From: Ontario, Canada
Registered: 2005-03-17
Posts: 75
Website

Re: Help a PHP proto-newbie

Thanks for your help!  I'll try this out when I get a chance.  It looks to be exactly what I need.

Offline

Board footer

Powered by FluxBB