[英]Remove string from the start of specific string in PHP
I have an url string like this in php:我在 php 中有这样的 url 字符串:
$string = 'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441';
i want to remove specific string from price
query value in the URL string that started with `%2c.我想从price
字符串中以 `%2c. for example:例如:
test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441
into
test.xyz/builder-list/?price=301-500&builder_country=6442%2C6780%2C6441
test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445
into
test.xyz/builder-list/?price=-200&builder_region=12%2C33
test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500
into
test.xyz/builder-list/?builder_state=45%2C76&price=-200
i tried to use this preg_replace
function, but it deletes all the %2C
string我尝试使用这个preg_replace
function,但它删除了所有%2C
字符串
preg_replace('/' . preg_quote('%2C') . '.*?/', '', $string);
If you are using regex, you have to capture price
specifically and capture the former and latter parts of the delimiter %2C
into separate regex groups and replace them.如果您使用正则表达式,则必须专门捕获price
并将分隔符%2C
的前后部分捕获到单独的正则表达式组中并替换它们。 It would look like below:它如下所示:
preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $str)'
-------- ------- ----
Grp 1. Grp 2. Only grp 1 and 2.
Snippet:片段:
<?php
$tests = [
'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441',
'test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445',
'test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500',
'test.xyz/builder-list/?builder_state=45%2C76&price=%2C400-500'
];
foreach($tests as $test){
echo preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $test),PHP_EOL;
}
Demo: http://sandbox.onlinephpfunctions.com/code/f5fd3acba848bc4f2638ea89a44c493951822b80演示: http://sandbox.onlinephpfunctions.com/code/f5fd3acba848bc4f2638ea89a44c493951822b80
Another alternative from regex is to break down the query string via parse_str
.正则表达式的另一种选择是通过parse_str
分解查询字符串。
Use the first strtok
to get the base url and separate it so that you can feed the query string params in parse_str
.使用第一个strtok
获取基础 url 并将其分开,以便您可以在parse_str
中提供查询字符串参数。
After you have separated it and loaded it into parse_str
, then you can make changes on the individual parts of the query string.将其分离并将其加载到parse_str
后,您可以对查询字符串的各个部分进行更改。 If you want to make changes on the price, then manipulate it like so.如果您想更改价格,请像这样操纵它。
Use another strtok
just to effectively trim characters after ,
or ( %2C
) and reassign.使用另一个strtok
只是为了有效地修剪,
or ( %2C
) 之后的字符并重新分配。
Finally, reattach the query strings back using http_build_query
concatenated by the separated base url in the earlier operation.最后,在前面的操作中使用由分离的基础 url 连接的http_build_query
重新附加查询字符串。
$string = 'test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445';
$base_url = strtok($string, '?');
parse_str(str_replace("{$base_url}?", '', $string), $data);
$data['price'] = strtok($data['price'], ',');
$final_string = "{$base_url}?" . http_build_query($data);
echo $final_string;
$string = 'test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500';
//Separate string based on & an make an array $q
$q = explode('&', $string);
//Go through each item in array $q and make adjustments
//if it's the price-query
foreach($q as &$item) {
if (stristr($item,'price') !== false) {
//Just leave left the first part of
//this item before %2C
$pos = strpos($item, '%2C');
$item = substr($item,0,$pos);
break; //No need being here in this loop anymore
}
}
//Implode back to original state and glue it together with ampersand
$result = implode('&', $q);
$result
would contain: $result
将包含:
test.xyz/builder-list/?builder_state=45%2C76&price=-200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.