php – Regular Expression Schnipsel – Links aus -Tags

closeThis post was published more than three months ago. Please note that the information offered here may no longer be current and valid. Therefore, please inform yourself about this topic elsewhere. If there is any new information, you can also send me a comment. Thank you so much!

Habe zu dem Thema (“Reguläre Ausdrücke”) bereits ausführlicher geschrieben -> http://suckup.de/howto/php-howto/php-sicherheit-erhoehen-teil-2/ <- daher folgt hier einfach ein Beispiel, welches man sicherlich noch optimieren kann … :)

<?php
$lall =' lall lall123
<a style="gfsedfd;" href="http://test1.de">test
<a style="gfsedfd;" href="test2.php" target="_blank>test</a> dsadsa
<a style="gfsedfd;" href="http://test3.de" target="_blank>test</a>
dsads
';

$lall = str_ireplace("\r", '', $lall);
$lall = str_ireplace("\n", '', $lall);
$lall = str_ireplace("\t", '', $lall);
preg_match_all('/<a [^>].*href="(.*)".*>/i', $lall, $lall_new);
$allLinks = explode("</a>", $lall_new[0][0]);
foreach ($allLinks as $key => $val) {
  $val = trim($val);
  if ($val != '') {
    $data =preg_replace('/.*<a [^>].*href\s*=\s*(["\'])(.*?)\1.*>.*/i',"$2",$val);
    echo $data . "\n";
  }
}
?>
php testtest.php

http://test1.de
test2.php
http://test3.de

Published by

voku

Lars Moelleken | Ich bin root, ich darf das!