2

I have an url string like this in php:

$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. 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('/' . preg_quote('%2C') . '.*?/', '', $string);
4
  • why? it's just percent encoded, just simply use urldecode, no need to regular expressions Commented Jun 12, 2020 at 3:46
  • here's an example tehplayground.com/3if2h2QE2iCbYPVm Commented Jun 12, 2020 at 3:47
  • @Kevin i only need the first value from the price query parameter in the string URL , that's why i need to replace the %2C and the rest of the value from price query parameter in my URL string Commented Jun 12, 2020 at 3:51
  • Just for the sake if it: I guess test.xyz/builder-list/?price=-200&builder_region=12%2C33 is a typo and should be test.xyz/builder-list/?price=-200&builder_region=1223%2C3445 Commented Jun 12, 2020 at 5:22

3 Answers 3

2

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. 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

Sign up to request clarification or add additional context in comments.

1 Comment

One line without any hassle! Awesome solution!
1

Another alternative from regex is to break down the query string via 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.

After you have separated it and loaded it into parse_str, then you can make changes on the individual parts of the query string. 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.

Finally, reattach the query strings back using http_build_query concatenated by the separated base url in the earlier operation.

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

2 Comments

thanks it works, but i came accross some issue when i have an URL string like this: http://test.xyz/builder-list/?price=201-500%2C201-500&amp;builder_country=6356%2C6699 it will be converted into this: http://test.xyz/builder-list/?price=201-500&amp%3Bbuilder_country=6356%2C6699 when i print in the browser it has %3B in the string
@jojo you must be double encoding somewhere, &amp; is for html
1
$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:

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

4 Comments

ah sorry! Thanks @vivek_23 . I'll adjust
I've updated my code so now it works as I think OP would expect :-)
Hmm. I think you can also add a break in the if condition once it's done.
@vivek_23 - yes that's true! It would work anyway but it's always better to do it as right as you can :-) I added the break. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.