M
M
michaelromanov902018-02-03 08:47:27
PHP
michaelromanov90, 2018-02-03 08:47:27

How to remove or replace a broken link using DomDocument?

Tell me how to remove or replace a "broken" link through DomDocument
so that there is something like this:

<a href="http://yandex.ru"></a>
<a href=""></a>
<a href="">asdasd</a>
<a href="" target="_blank">asdasd</a>
<a></a>
<a><p>......

In the DomDocument, I'm looking for tags without the href property, for example, and then I try to remove this link, but not everything that needs to be removed is due to (as I think, due to the fact that there can be more than 1 child in the parent and when deleting, only 1st)
foreach ($links as $link){
    if($link->getAttribute('href') == false){
        $link->parentNode->removeChild($link);
    }
}

asdasd asdasdasdas dasd <a target="_blank">link</a> asdasdasdsadasda sdas dasd asdasd sad <a target="_blank">являются</a> sadsadsad

only the 1st link will be deleted, because they have 1 parent. If I go through the cycle again, the 2nd one will also be deleted, but I don’t know how many links the 1st parent can have .. Tell me how to be pliz)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Padre, 2018-02-03
@yii16

if(strpos($mystring, 'href=""') !== false || strpos($mystring, '></a>') !== false){
    echo 'в строке есть пустая ссылка или пустой url';
}

A
Alexander, 2018-02-03
@SAnhPa

https://regex101.com/r/UczLYE/2

all = document.querySelectorAll('a[href=""]');
all.forEach(function(el) {
  el.remove();
});

X
xmoonlight, 2018-02-03
@xmoonlight

It's simple:

  1. We first get all href strings from the HTML markup text: https://regex101.com/r/sCOA59/2/
  2. We find all matches ("broken" links) and replace them using preg_replace: https://regex101.com/r/G8mCYo/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question