$post_id = 228;
$content_post = get_post($post_id);
$content = $content_post->post_content;
$doc = new DOMDocument();
$doc->loadHTML($content);
$links = $doc->getElementsByTagName('a');
foreach ($links as $link){
$href = $link->getAttribute('href');
$avoid = array('.jpg', '.png', '.gif', '.jpeg');
if (strpos($href, $avoid) == false) {
echo $link->nodeValue, '<br>';
echo $href, '<br>';
}
}
strpos is still returning the urls when false - any idea what I am missing? !==
doesn't work either.
Added info: I am trying to exclude all image urls, so if you have a better way - feel free to share.
Update, does not do the trick either . (tried this using the documentation).
foreach ($links as $link){
$href = $link->getAttribute('href');
$avoid = array('.jpg', '.png', '.gif', '.jpeg');
$pos = strpos($href, $avoid);
if ($pos === false) {
echo $link->nodeValue, '<br>';
echo $href, '<br>';
}
}
If you want to check if $href
contains any of the strings in $avoid
, you can replace them with nothing and see if you still have the original string.
if ($href == str_replace($avoid, '', $href)) { ...
str_replace
can take an array, unlike strpos
.