php - Adding a target to link tags, as long as the href attribute doesn't contain a certain word -


i created function:

<?php     function target_links( $html )     {                $pattern = "/<(a)([^>]+)>/i";         $replacement = "<\\1 target=\"_blank\"\\2>";         $new_str = preg_replace($pattern,$replacement,str_replace('target="_blank"','',$html));              return $new_str;     } ?> 

the goal add target="_blank" link tags.

now problem need skip link tags href attribute contains specific word, can't seem find proper combination. can guys me?

i'm not sure "not failing because of broken html", if can domdocument accept html, try like:

<?php $dom = new domdocument(); $dom->loadhtml('<html>     <a href="...protected...">some link</a>     <a href="...change me...">some link</a> </html>');  $xpath = new domxpath($dom); foreach ($xpath->query('//a[not(contains(@href, "protected"))]') $node) {     $node->setattribute('target', '_blank'); }  header('content-type: text/html; charset="utf-8"'); echo $dom->savehtml(); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -