从 PHP 中特定字符串的开头删除字符串

我在 php 中有一个这样的 url 字符串:


$string = 'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441';

我想从price以 `%2c 开头的 URL 字符串中的查询值中删除特定字符串。例如:


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

我尝试使用这个preg_replace函数,但它删除了所有的%2C字符串


preg_replace('/' . preg_quote('%2C') . '.*?/', '', $string);


摇曳的蔷薇
浏览 189回答 3
3回答

慕码人2483693

如果您使用的是正则表达式,则必须price专门捕获并将分隔符的前后部分捕获%2C到单独的正则表达式组中并替换它们。它看起来像下面这样:preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $str)'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-------- -------&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;----&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Grp 1.&nbsp; &nbsp;Grp 2.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Only grp 1 and 2.片段:<?php$tests = [&nbsp; &nbsp; &nbsp; &nbsp; 'test.xyz/builder-list/?price=301-500%2C501-1000&builder_country=6442%2C6780%2C6441',&nbsp; &nbsp; &nbsp; &nbsp; 'test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445',&nbsp; &nbsp; &nbsp; &nbsp; 'test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500',&nbsp; &nbsp; &nbsp; &nbsp; 'test.xyz/builder-list/?builder_state=45%2C76&price=%2C400-500'&nbsp; &nbsp; ];foreach($tests as $test){&nbsp; &nbsp; echo preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $test),PHP_EOL;}演示: http://sandbox.onlinephpfunctions.com/code/f5fd3acba848bc4f2638ea89a44c493951822b80

慕桂英3389331

$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);&nbsp;//Go through each item in array $q and make adjustments//if it's the price-queryforeach($q as &$item) {&nbsp; &nbsp; if (stristr($item,'price') !== false) {&nbsp; &nbsp; &nbsp; &nbsp; //Just leave left the first part of&nbsp; &nbsp; &nbsp; &nbsp; //this item before %2C&nbsp; &nbsp; &nbsp; &nbsp; $pos = strpos($item, '%2C');&nbsp; &nbsp; &nbsp; &nbsp; $item = substr($item,0,$pos);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; break; //No need being here in this loop anymore&nbsp; &nbsp; }}//Implode back to original state and glue it together with ampersand$result = implode('&', $q);$result将包含:test.xyz/builder-list/?builder_state=45%2C76&price=-200

郎朗坤

正则表达式的另一种选择是通过parse_str.使用第一个strtok获取基本 url 并将其分开,以便您可以在parse_str.在将其分离并加载到 中之后parse_str,您可以对查询字符串的各个部分进行更改。如果您想更改价格,请像这样操纵它。使用另一个只是为了有效地修剪or ( )strtok之后的字符并重新分配。,%2Chttp_build_query最后,使用之前操作中分离的基本 url 连接的方式重新附加查询字符串。$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 =&nbsp; "{$base_url}?" . http_build_query($data);echo $final_string;
打开App,查看更多内容
随时随地看视频慕课网APP