如何清除字符串php中的某些字符后的所有内容?
我有消毒一切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]);
您应该使用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);
Array
(
[0] => http://site/something/another/
[1] => http://site/something/another2/
[2] => http://site/something/another/
[3] => http://site/something/another/?foo=bar
)
如果你试图解析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
}
不能使用PECL – Benn 2012-04-28 01:47:42
@Benn我已经删除了'http_build_url'调用,它需要PECL。这个版本不够健壮,但它应该适合你的需求。 – 2012-04-28 01:59:07
所以你想删除或替换'='后面的所有内容吗? – Sampson 2012-04-28 01:27:45
@Jonahtan Samplson在get_something = – Benn 2012-04-28 01:29:54
之后如何使用parse_url()解析url并清理“query”数组值? – 2012-04-28 01:30:08