Page 1 of 1

basic String question

Posted: Sun Aug 21, 2005 2:28 pm
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..

Posted: Sun Aug 21, 2005 2:35 pm
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);

Posted: Sun Aug 21, 2005 3:16 pm
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

Posted: Sun Aug 21, 2005 5:54 pm
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 :)

Posted: Sun Aug 21, 2005 5:58 pm
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.