第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 PHP 中特定字符串的開頭刪除字符串

從 PHP 中特定字符串的開頭刪除字符串

PHP
搖曳的薔薇 2023-05-26 10:01:32
我在 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%2C6441intotest.xyz/builder-list/?price=301-500&builder_country=6442%2C6780%2C6441test.xyz/builder-list/?price=-200%2C400-500&builder_region=1223%2C3445intotest.xyz/builder-list/?price=-200&builder_region=12%2C33test.xyz/builder-list/?builder_state=45%2C76&price=-200%2C400-500intotest.xyz/builder-list/?builder_state=45%2C76&price=-200我嘗試使用這個preg_replace函數(shù),但它刪除了所有的%2C字符串preg_replace('/' . preg_quote('%2C') . '.*?/', '', $string);
查看完整描述

3 回答

?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

如果您使用的是正則表達式,則必須price專門捕獲并將分隔符的前后部分捕獲%2C到單獨的正則表達式組中并替換它們。它看起來像下面這樣:


preg_replace('/(price\=)([^&]*)%2C[^&]*/', '$1$2', $str)'

               -------- -------             ----

                Grp 1.   Grp 2.              Only grp 1 and 2.

片段:


<?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;

}

演示: http://sandbox.onlinephpfunctions.com/code/f5fd3acba848bc4f2638ea89a44c493951822b80


查看完整回答
反對 回復 2023-05-26
?
慕桂英3389331

TA貢獻2036條經(jīng)驗 獲得超8個贊

$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將包含:


test.xyz/builder-list/?builder_state=45%2C76&price=-200


查看完整回答
反對 回復 2023-05-26
?
郎朗坤

TA貢獻1921條經(jīng)驗 獲得超9個贊

正則表達式的另一種選擇是通過parse_str.


使用第一個strtok獲取基本 url 并將其分開,以便您可以在parse_str.


在將其分離并加載到 中之后parse_str,您可以對查詢字符串的各個部分進行更改。如果您想更改價格,請像這樣操縱它。


使用另一個只是為了有效地修剪or ( )strtok之后的字符并重新分配。,%2C


http_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 =  "{$base_url}?" . http_build_query($data);

echo $final_string;


查看完整回答
反對 回復 2023-05-26
  • 3 回答
  • 0 關注
  • 227 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號