basic String question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

basic String question

Post by d3ad1ysp0rk »

Feel pretty stupid right now, but I need a bit of help.

Without using regex (since I don't know it, and I'm assuming no one would help me write it), how would you go about getting the value of a and b in the following:

Code: Select all

<html>
<body>
<table>
<tr>
<td><h3>var</h3></td><td>value</td></tr>
<td><h3>a</h3></td>
<td>37</td>
</tr><tr>
<td><h3>b</h3></td>
</tr>
</table>
</body>
</html>
I loaded it into a variable using file_get_contents, then thought about using strpos, but couldn't get it to work.

Thanks for the help. I need to get more sleep..
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

I know you said you didn't want to use regex, but honestly I think it's the best solution.

might try something like this:

Code: Select all

preg_match_all("/<td>(\d*)<\/td>/",$yourstring,$matches);
print_r($matches);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Burr needs more training :P

Code: Select all

preg_match_all('#<(h[1-6])[^>]*?>(.*?)</\\1[^>]*?>#is',$text,$matches);
var_export($matches);
not the most flexible, but you didn't say anything about acceptance information :P
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

preg_match_all('#<(h[1-6])[^>]*?>(.*?)</\\1[^>]*?>#is',$text,$matches); 
var_export($matches);
I am not so good at regex but I can understand them but yours is a bit complex for me.
at certain places you have the ? which follows the *. wots the meaning of that???
wots the # and #is???
I hope you dont mind explaning us how this works :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

*? = ungreedy matching
# is a custom ending and starting delimeter (you don't have to use /)

Maybe you should read: http://us2.php.net/manual/en/reference. ... syntax.php

It's got everything. Even if you don't program PHP, PHP: Pattern Syntax is an extremely good, concise and comprehensive reference for regexps.
Post Reply