2012-04-28 112 views
2

我有消毒一切GET和POST,但是這一次一定要更具體一點幾的功能,我有一個是生產像如何清除字符串php中的某些字符後的所有內容?

array(
    1 => http://site/something/another/?get_something=1 
    2 => http://site/something/another2/?get_something=1 
) 

鏈接陣列也有是消毒,得到了功能和職位,但該所以我留下功能會從該數組值清除一切

httpsitesomethinganotherget_something1

能有人幫get_something= 之後匹配的一切,清除或get_something = 1替換它,所以mething像

$clear = preg_match('/^[A-Za-z0-9]+$/', $array[1]); 
preg_replace("/get_something=".$clear."/i","",$array[1]); 
+0

所以你想刪除或替換'='後面的所有內容嗎? – Sampson 2012-04-28 01:27:45

+0

@Jonahtan Samplson在get_something = – Benn 2012-04-28 01:29:54

+4

之後如何使用parse_url()解析url並清理「query」數組值? – 2012-04-28 01:30:08

回答

1

您應該使用parse_url但這裏是一個正則表達式的實現,如果你很好奇:

$urls = array 
(
    'http://site/something/another/?get_something=1', 
    'http://site/something/another2/?get_something=1', 
    'http://site/something/another/?get_something=1&foo=bar', 
    'http://site/something/another/?foo=bar&get_something=1', 
); 

$urls = preg_replace('~[?&]get_something=.*$~i', '', $urls); 

print_r($urls); 

應該輸出(demo [updated):

Array 
(
    [0] => http://site/something/another/ 
    [1] => http://site/something/another2/ 
    [2] => http://site/something/another/ 
    [3] => http://site/something/another/?foo=bar 
) 
+0

這個人也清理一切還/get_something=(.*)/ – Benn 2012-04-28 17:28:26

+0

@Benn:哦,我的印象是,你想刪除關於'get_something'參數*的一切*。要刪除所有內容,它更簡單,請檢查我的編輯。 – 2012-04-28 17:48:15

+0

完美謝謝! – Benn 2012-04-28 19:19:42

0

如果你試圖解析URL,你最好的辦法是使用parse_url,砍掉你不想要的查詢元素,並與http_build_url重建。以下是我想做到這一點:

for($i = 0; $i < count($linkArray); $i++) { 
    // get original link and parse it 
    $link = parse_url($linkArray[$i]); 

    // replace the query portion 
    $link["query"] = "get_something=1"; 

    // build the new URL 
    $linkArray[$i] = $link["scheme"] . "://" . $link["hostname"] . $link["path"] . "?" . $link["query"] . "#" . $link["fragment"] 

    // done 
} 
+0

不能使用PECL – Benn 2012-04-28 01:47:42

+0

@Benn我已經刪除了'http_build_url'調用,它需要PECL。這個版本不夠健壯,但它應該適合你的需求。 – 2012-04-28 01:59:07

相關問題