Trying to get all the title="" vales of an area's but its only picking the the main title of the page. Sample area html:
<area shape="poly" title="Orkney" href="orkney" coords="" />
The page has lots of these and i'm trying to get all the title names My PHP:
function getTextBetweenTags($string, $tagname) {
// Create DOM from string
$html = str_get_html($string);
$titles = array();
// Find all tags
foreach($html->find($tagname) as $element) {
$titles[] = $element->plaintext;
}
return $titles;
}
print_r(getTextBetweenTags($url, 'title'));
All i get is the title of the page, is this the correct way, or can it not be done, thanks for any help :)
call the method like this:
print_r(getTextBetweenTags($url, 'area'));
since you're searching for area tags. The title is an argument. See this documentation for an idea how to do it.
You need to look for <area>
elements and return their title
attribute
function getTextBetweenTags($string, $tagname) {
// Create DOM from string
$html = str_get_html($string);
$titles = array();
// Find all tags
foreach($html->find($tagname) as $element) {
$titles[] = $element->title;
}
return $titles;
}
print_r(getTextBetweenTags($url, 'area'));
And if you using the $url for the paramenter, shouldn't you be using file_get_html()
instead of str_get_html()
?