2016-06-29 3 views
0

Ich habe riesige online PHP-Dateisatz, der dynamisch gemacht wird. Es hat Links, auch einige ungültig diejenigen mit Anführungszeichen (hergestellt mit der Startseite)Wie preg_replace Links, so dass der Hash beibehalten wird

index2.php?page=xd 
index2.php?page=xj asdfa 
index2.php?page=xj%20aas 
index2.php?page=xj#jumpword 
index2.php?page=gj#jumpword with spaces that arenot%20 
index2.php?page=afdsdj#jumpword%20with 
index2.php?page=xj#jumpword with "quotes" iknow 


$input_lines=preg_replace("/(index2.php?page\=.*)(#[a-zA-Z0-9_ \\"]*)(\"\>)/U", "$0 --> $2", $input_lines); 

ich alle diejenigen, will mit der # -Teil sein gerade und nicht den index2.php hat? Page = * Teil.

Ich konnte den ganzen Abend nicht arbeiten. Also bitte hilf mir.

+0

' preg_replace ("/^index2.p hp? page \ = [^ #] */"," ", $ input_lines)' –

Antwort

0

In einigen Fällen können Sie parse_url verwenden Attribute aus der URL zu erhalten (zB: was nach dem # ist), etwa so:

$urls = array(
    'index2.php?page=xd', 
    'index2.php?page=xj asdfa', 
    'index2.php?page=xj%20aas', 
    'index2.php?page=xj#jumpword', 
    'index2.php?page=gj#jumpword with spaces that arenot%20', 
    'index2.php?page=afdsdj#jumpword%20with', 
    'index2.php?page=xj#jumpword with "quotes" iknow', 
    ); 

foreach($urls as $url){ 
    echo 'For "' . $url . '": '; 
    $parsed = parse_url($url); 
    echo isset($parsed['fragment']) ? $parsed['fragment'] : 'DID NOT WORK'; 
    echo '<br>'; 
} 

Ausgang:

Für „index2. php page = xd? ": hat nicht funktioniert

Für "index2.php page = xj asdfa": hat nicht funktioniert

F oder "? index2.php page = xj% 20aas": "index2.php page = xj # jumpword" funktioniert nicht

Für: jumpword

Für „index2.php page = gj # jumpword mit? Räume, die% 20" arenot: jumpword mit Räumen, die% arenot 20

Für "index2.php page = afdsdj # jumpword% 20with?": jumpword% 20with

Für „index2.php page = xj #? Sprungwort mit "Anführungszeichen" iknow ": Sprungwort mit" Anführungszeichen "iknow

+0

Keine Rückmeldung vom op .. ich nehme an es ist fest xD – FirstOne

Verwandte Themen