1

I have a HTML page that includes the code

<li><span class="li-time">00:30</span>
<span class="li-title">Headline News</span></li>
<li><span class="li-time">00:31</span>
<span class="li-title">Money Mind</span></li>
<li><span class="li-time">01:00</span>
<span class="li-title">Headline News</span></li>
<li><span class="li-time">01:01</span>
<span class="li-title">Singapore Tonight</span></li> 

and does not have a definite number of how many values there are. What I want to do is to format the result so that it displays all of the time/title in the format

00:30 Headline News
00:31 Money Mind
01:00 Headline News

It would be best if I didn't have to use an external parser. Sorta new to php, hope you guys can advise me :)

8
  • So you have a bunch of times, and a bunch of names, and they are associated with each other on a 1-to-1 basis? This data should be expressed as a table. Commented Feb 7, 2011 at 7:35
  • 1
    Why do you want to use PHP to transform HTML? Use CSS to style the page into the presentation you want. Commented Feb 7, 2011 at 7:36
  • 2
    I'm not really sure what the question is though. Is that HTML the input or the output? Where does PHP come into it? Do you want to parse the HTML with PHP, and then output a plain text file with the data? Commented Feb 7, 2011 at 7:36
  • That's HTML, not php. I would suggest finding a good online tutorial on starting php Commented Feb 7, 2011 at 7:36
  • 1
    (related) Best Methods to parse HTML Commented Feb 7, 2011 at 7:58

3 Answers 3

3

Thanks for all the suggestions guys, managed to solve my problem using DOM

<?php


$data = file_get_contents("html");


$pattern = '/<ul id="schedule">.*<\/ul>/';
preg_match($pattern, $data, $matches); // to obtain sample code (<li><span...>)


function getTextBetweenTags($string, $tagname){
    $d = new DOMDocument();
    $d->loadHTML($string);
    $return = array();
    foreach($d->getElementsByTagName($tagname) as $item){
        $return[] = $item->textContent;
    }
    return $return;
}

$count = 0;
$txt = getTextBetweenTags($matches[0], "span");
foreach ($txt as $val){
$count++;
echo $val;
echo "\t";
if (($count % 2) == 0){
    print "<br>";
}
}

?>
Sign up to request clarification or add additional context in comments.

Comments

0

In this simple case, just call $text = strip_tags($html). Then $text looks like this:

00:30
Headline News
00:31
Money Mind
01:00
Headline News

Now do something like this to replace every other newline with a space:

$lines = explode('\n',$text);
$final_text = '';
for ($i=0;$i<count($lines);$i++) {
  $final_text .= $lines[$i];
  if ($i%2==0) {
     $final_text .= ' ';
  } else {
     $final_text .='\n';
  }
}

If the HTML you have is not always precisely the format you have shown, you will have to parse the HTML using SimpleXML or the more complicated DOM functions.

An example using SimpleXML would be the following:

$html = <<<EOF
<html>
<li><span class="li-time">00:30</span>
<span class="li-title">Headline News</span></li>
<li><span class="li-time">00:31</span>
<span class="li-title">Money Mind</span></li>
<li><span class="li-time">01:00</span>
<span class="li-title">Headline News</span></li>
<li><span class="li-time">01:01</span>
<span class="li-title">Singapore Tonight</span></li> 
</html>
EOF;

$sx = simplexml_load_string($html);

$output_text = '';
foreach($sx->li as $list_item) {
    $output_text .= $list_item->span[0] . " " . $list_item->span[1] . "\n";
}

echo $output_text;

Please note that $html must be wrapped with some tag (I chose <html></html> in the example).

2 Comments

I've tried the split_tags function already, but the results actually turn out to be 00:30Headline News00:31Money Mind01:00Headline News instead of new lines
Oh... Have you tried adding a space before every newline? Eg. str_replace('\n',' \n',$text)
0

If you just want to control layout and typography, you have to use CSS, not PHP.

1 Comment

So I won't be able to format it using php even if I've stored the HTML code in a variable? I'm getting the results(time/title) from another site, so it's not possible to use HTML/CSS for formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.