php regex optional field -
i seem have confused myself preg_match regex i'm doing, fresh eyes , appreciated.
my current regex follows:
"#table.*?stream status:.*?<b>stream @ (.*?) kbps (.*?) of (.*?) listeners</b>.*?listener peak.*?<b>(.*?)</b>.*?stream name.*?<b>(.*?)</b>.*?content type.*?<b>(.*?)</b>.*?stream genre.*?<b>(.*?)</b>.*?stream url.*?<b>(.*?)</b>.*?current song.*?<b>(.*?)</b>.*?</table#si"
but columns not given, best solution mark field optional?
the table looks like:
any advice nice ;-)
you shouldn't use regular expressions parse html.
here's quick script create array filled keys , values:
<?php $str = <<< endl <table cellpadding="2" cellspacing="0" border="0" align="center"> <tr> <td width="120" valign="top">server status: </td> <td><b>server , public</b></td> </tr> <tr> <td valign="top">stream status: </td> <td><b>stream @ 256 kbps 0 of 250 listeners</b></td> </tr> <tr> <td valign="top">listener peak: </td> <td><b>1</b></td> </tr> <tr> <td valign="top">average listen time: </td> <td><b>1 hours 50 minutes 8 seconds</b></td> </tr> <tr> <td valign="top">stream name: </td> <td><b><a target="_blank" href="http://www.shoutcast.com/internet-radio/xyz">xyz</a></b></td> </tr> <tr> <td valign="top">content type: </td> <td><b>audio/aacp</b></td> </tr> <tr> <td valign="top">stream genre: </td> <td><b>jazz</b></td> </tr> <tr> <td valign="top">stream url: </td> <td><b><a target="_blank" href="http://tmp/">http://tmp/</a></b></td> </tr> <tr> <td valign="top">current song: </td> <td><b><a href="currentsong?sid=1">test</a></b></td> </tr> <tr> <td valign="top">next song: </td> <td><b><a href="nextsong?sid=1">test2</a></b></td> </tr> </table> endl; $html = new simplexmlelement($str); $buffer = array(); foreach($html->tr $row) { $label = trim(rtrim(trim($row->td[0]), ":")); $value = trim(strip_tags($row->td[1]->asxml())); $buffer[$label] = $value; } echo "<pre>"; var_dump($buffer); echo "</pre>";
result:
array(10) { ["server status"]=> string(33) "server , public" ["stream status"]=> string(48) "stream @ 256 kbps 0 of 250 listeners" ["listener peak"]=> string(1) "1" ["average listen time"]=> string(28) "1 hours 50 minutes 8 seconds" ["stream name"]=> string(3) "xyz" ["content type"]=> string(10) "audio/aacp" ["stream genre"]=> string(4) "jazz" ["stream url"]=> string(11) "http://tmp/" ["current song"]=> string(4) "test" ["next song"]=> string(5) "test2" }
Comments
Post a Comment