Reputation: 2347
I store the content of a website in a string $html.
I want to count all html links that link to a file in the .otf format, add a list of these links to the end of $html and remove the original links.
An example:
<?php
$html_input = '
<p>
Lorem <a href="font-1.otf">ipsum</a> dolor sit amet,
consectetur <a href="http://www.cnn.com">adipiscing</a> elit.
Quisque <a href="font-2.otf">ultricies</a> placerat massa
vel dictum.
</p>'
// some magic here
$html_output = '
<p>
Lorem ipsum dolor sit amet,
consectetur <a href="http://www.cnn.com">adipiscing</a> elit.
Quisque ultricies placerat massa
vel dictum.
</p>
<p>.otf-links: 2</p>
<ul>
<li><a href="font-1.otf">ipsum</a></li>
<li><a href="font-2.otf">ultricies</a></li>
</ul>'
?>
How do I do that? Should I use regular expressions, or is there another way?
Upvotes: 0
Views: 1977
Reputation: 31300
require_once("simple_html_dom.php");
$doc = new simple_html_dom();
$doc->load($input_html);
$fonts = array();
$links = $doc->find("a");
foreach ( $links as $l ) {
if ( substr($l->href, -4) == ".otf" ) {
$fonts[] = $l->outertext;
$l->outertext = $l->innertext;
}
}
$output = $doc->save() . "\n<p>.otf-links: " . count($fonts) ."</p>\n" .
"<ul>\n\t<li>" . implode("</li>\n\t<li>", $fonts) . "</li>\n</ul>";
Documenation for Simple HTML DOM http://simplehtmldom.sourceforge.net/
Upvotes: 5
Reputation: 55445
Use a DOM Parser
Example:
$h = str_get_html($html);
$linkCount = count($h->find('a'));
foreach ( $h->find('a') as $a ){
//print every link ending in .odf
if ( ends_with(strtolower($a->href), '.odf') ){ //ends with isn't a function, but it is trivial to write
echo '<li><a href="'.$a->href.'">'.$a->innertext.'</a></li>';
}
}
Upvotes: 2
Reputation: 5291
preg_match('~<a href="[^"]+\.otf">.*?</a>~s', $html_input, $matches);
$linksCount = count($matches[0]);
preg_replace('~<a href="[^"]+\.otf">.*?</a>~s', '', $html_input);
$html_input.='<ul><li>'.implode('</li><li>', $matches[0]).'</li></ul>';
Upvotes: -1