1

I seem to have confused myself with a preg_match regex I'm doing, so fresh eyes and help would be appreciated.

My current regex is as follows:

"#table.*?Stream Status:.*?<b>Stream is up at (.*?) kbps with (.*?) 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 sometimes some columns are not given, so what is the best solution to mark a field as optional?

the table looks like:

http://pastebin.com/wBRXhJDP

any advice would nice ;-)

jimbo
  • 45
  • 2
  • 7
  • 1
    Regex shouldn't be used to parse html. Check out this post http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags checkout the answer. – Liam Sorsby Jul 23 '15 at 15:31

1 Answers1

0

You shouldn't use regular expressions to parse HTML.

Here's a quick script to create an array filled with keys and 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 is currently up and public</b></td>
    </tr>
    <tr>
        <td valign="top">Stream Status: </td>
        <td><b>Stream is up at 256 kbps with 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 as $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 is currently up and public"
  ["Stream Status"]=>
  string(48) "Stream is up at 256 kbps with 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"
}
Community
  • 1
  • 1
Rob W
  • 9,134
  • 1
  • 30
  • 50