2010-09-26 6 views
1

J'ai une chaîne en PHP qui est un URI avec tous les arguments:supprimer une partie d'une chaîne d'argument URL en php

$string = http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0 

Je veux supprimer complètement un argument et retourne la chaîne reste. Par exemple, je veux supprimer arg3 et finissent avec:

$string = http://domain.com/php/doc.php?arg1=0&arg2=1 

Je veux toujours enlever le même argument (arg3), et il peut ou ne pas être le dernier argument.

Pensées?

EDIT: il pourrait y avoir un tas de personnages bizarres dans arg3 donc ma façon préférée de le faire (en substance) serait:

$newstring = remove $_GET["arg3"] from $string; 

Répondre

8

Il n'y a pas vraiment de raison d'utiliser des regex ici, vous pouvez utiliser des fonctions de type string et array à la place.

Vous pouvez explode la partie après la ? (que vous pouvez obtenir en utilisant substr pour obtenir une sous-chaîne et strrpos pour obtenir la position de la dernière ?) dans un tableau, et utiliser unset pour enlever arg3, puis join de mettre la chaîne de retour ensemble .:

$string = "http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0"; 
$pos = strrpos($string, "?"); // get the position of the last ? in the string 
$query_string_parts = array(); 

foreach (explode("&", substr($string, $pos + 1)) as $q) 
{ 
    list($key, $val) = explode("=", $q); 
    if ($key != "arg3") 
    { 
    // keep track of the parts that don't have arg3 as the key 
    $query_string_parts[] = "$key=$val"; 
    } 
} 

// rebuild the string 
$result = substr($string, 0, $pos + 1) . join($query_string_parts); 

Voir en action à http://www.ideone.com/PrO0a

+0

bien ...... travaillé –

0
preg_replace("arg3=[^&]*(&|$)", "", $string) 

Je suppose que l'url lui-même ne sera pas contiennent arg3= ici, ce qui dans un monde sain devrait être une hypothèse sûre.

0
$new = preg_replace('/&arg3=[^&]*/', '', $string); 
0

Cela devrait également travailler, en tenant compte, par exemple, des ancres de page (#) un d au moins certains de ces "personnages étranges" vous mentionnez, mais ne semble pas inquiet:

function remove_query_part($url, $term) 
{ 
    $query_str = parse_url($url, PHP_URL_QUERY); 
    if ($frag = parse_url($url, PHP_URL_FRAGMENT)) { 
     $frag = '#' . $frag; 
    } 
    parse_str($query_str, $query_arr); 
    unset($query_arr[$term]); 
    $new = '?' . http_build_query($query_arr) . $frag; 
    return str_replace(strstr($url, '?'), $new, $url); 
} 

Démo:

$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0'; 
$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1'; 
$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0#frag'; 
$string[] = 'http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0&arg4=4'; 
$string[] = 'http://domain.com/php/doc.php'; 
$string[] = 'http://domain.com/php/doc.php#frag'; 
$string[] = 'http://example.com?arg1=question?mark&arg2=equal=sign&arg3=hello'; 

foreach ($string as $str) { 
    echo remove_query_part($str, 'arg3') . "\n"; 
} 

Sortie:

http://domain.com/php/doc.php?arg1=0&arg2=1 
http://domain.com/php/doc.php?arg1=0&arg2=1 
http://domain.com/php/doc.php?arg1=0&arg2=1#frag 
http://domain.com/php/doc.php?arg1=0&arg2=1&arg4=4 
http://domain.com/php/doc.php 
http://domain.com/php/doc.php#frag 
http://example.com?arg1=question%3Fmark&arg2=equal%3Dsign 

Testé uniquement comme indiqué.

Questions connexes