Preg_match - récupérer href
-
19-09-2019 - |
Question
Je suis coincé avec une question que je viens aidé à -. Est un nouveau problème, mais seulement légèrement
J'ai ce preg_match pour obtenir le contenu de href. S'il vous plaît ne me dis pas utiliser regex - Je suis au courant de l'utilisation d'autres parseurs / etc, mais ce cours est un ancien script qui a juste besoin d'être fixé pour l'instant. :) Pas de temps pour re-écrit!
preg_match("~<a target=\'_blank\' rel=\'nofollow\' href=\"(.*?)\">~i", $epilink, $epiurl);
Il retourne:
http://www.example.com/frame2.php?view=&epi=54673-r
Cependant, il faut revenir:
http://www.example.com/frame2.php?view=168204&epi=54673
Ceci est un exemple de HTML, il travaillerait avec:
<a target='_blank' rel='nofollow' href="http://www.example.com/frame2.php?view=545903&epi=54683">
Pourquoi l'URL malformée Je suis revenu?
Merci à tous pour toute aide.
La solution
$string="<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
$s = explode('">',$string);
foreach($s as $k){
if (strpos($k,"href")!==FALSE){
echo preg_replace('/.*href="|/ms',"",$k);
break;
}
}
output
$ php test.php
http://www.example.com/frame2.php?view=545903&epi=54683
Autres conseils
Cela devrait fonctionner:
$epilink = "<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
preg_match("/<a target='_blank' rel='nofollow' href=\"(.*?)\">/i", $epilink, $epiurl);
print_r($epiurl);
vous pouvez également utiliser preg_match_all
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow