Search code examples
phphtmltitlesimple-html-dom

Only first word of title retrieved with PHP showing


So I am trying to display a list from another website in mine, it all works fine but only the first word of the 'title' attribute is stored. I know that the whole title is retrieved from the other website so how do I get it to store all of it.

Here is the code if it helps.

<?php
include "simple_html_dom.php";
$page = file_get_html("http://www.blade-edge.com/images/KSA/Flights/craft.asp?r=true&db=dunai");
echo "<table id=list>"; 
foreach($page->find('html/body/div/div[2]/ol/a') as $key=>$element) {
    if ($element->title != "No craft within this SOI"){
        $ships[$key] = $element->plaintext;
        $shipTitles[$key] = $element->title;
        $shipLinks[$key] = $element->href;
        echo "<tr>"; 
        echo "<td title =".$shipTitles[$key]." >";
        echo $ships[$key];
        echo "</td>";
        echo "</tr>";
    }
}
echo "</table>";
?>

enter image description here


Solution

  • Put title inside quotation marks

    echo "<td title =".$shipTitles[$key]." >";   // Wrong
    

    Right is

    echo "<td title ='".htmlspecialchars($shipTitles[$key], ENT_QUOTES)."' >";
    

    Without quotes, you will only see the word till the first space. Also, the value itself must be escaped.