2016-06-10 9 views
1

ich die folgende Regex mit dem href="http Teil innerhalb einer URL zu wählen, die keine rel="nofollow" noch nicht enthält:Nur die href übereinstimmen = "http innerhalb der Reges

preg_replace(
    "/<a\b(?=[^>]+\b(href=\"http))(?![^>]+\brel=\"nofollow\")/', 
    "rel=\"nofollow\" href=\"http://", 
    $input_string 
); 

Die Sache, die es nur ersetzt die <a weil dass das erste Spiel ist.

Wie ist es möglich, die einen Tag auswählen, aber den <a Teil aus den Ergebnissen ausschließen, so wird es nur href="http Vorstellungen? weil preg_match<a UND href="http zurückkehrt, aber ich nur ne href="http ed :)

Der Grund denke ich, könnte dies die einzig richtige Lösung ist, weil es nicht sicher ist, wie viele <a> Tag der String enthält und ob sie enthalten eine rel=nofollow oder nicht. Ich muss sicherstellen, dass ich nur ohne rel="nofollow"

EDIT 1 die http:// mit rel="nofollow" http:// innen <a> Tags ersetzen:

giuseppe straziota für Ein- und Ausgabe Beispiel so hier gefragt ist:

Eingangs :

this is a string with a lot of content and <a href="http://information.nl" class="aClass">links</a> and whatever.... 

Ausgabe:

this is a string with a lot of content and <a rel="nofollow" href="http://information.nl" class="aClass">links</a> and whatever.... 

EDIT 2:

ich ein paar mehr Tests durchgeführt, das sind die Ergebnisse:

Code (genaues Kopieren/Einfügen):

$input_string = 'this is a string with a lot of content and <a href="http://information.nl" class="aClass">links</a> and whatever....'; 

$input_string = preg_replace(
    '/<a\b(?=[^>]+\b(href="http))(?![^>]+\brel="nofollow")/', 
    'rel="nofollow" href="http://', 
    $input_string 
); 

echo htmlentities($input_string); 

Ergebnis von PHP 7.0 .5:

this is a string with a lot of content and rel="nofollow" href="http:// href="http://information.nl" class="aClass">links</a> and whatever.... 

Und es sollte sein:

this is a string with a lot of content and <a rel="nofollow" href="http://information.nl" class="aClass">links</a> and whatever.... 

EDIT 3:

Ich habe versucht, diese Regex:

$test = preg_replace(
    '/(?=<a\b[^>]+\b(href="http))(?![^>]+\brel="nofollow")/', 
    'rel="nofollow" href="http://', 
    $input_string 
); 

Aber jetzt setzt es die 'rel="nofollow" href="http://', direkt vor dem <a, so das Ergebnis:

rel="nofollow" href="http://<a href="http://information.nl" class="aClass">links</a> 

Nicht genau was ich will eit ihr ...

+0

Können Sie bitte ein Beispiel für die Eingabe und Ausgabe einfügen? –

+0

Ich habe ein Beispiel für die Eingabe und Ausgabe hinzugefügt;) –

+0

Ich hoffe, das Problem zu überstehen, hier finden Sie meinen Test https://regex101.com/r/bI2qQ0/1 mit mehr Tags in der Zeichenfolge arbeiten –

Antwort

Verwandte Themen