jojo
jojo

Reputation: 134

Remove string from the start of specific string in PHP

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

Upvotes: 2

Views: 138

Answers (3)

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

$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

Upvotes: 1

nice_dev
nice_dev

Reputation: 17825

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

Upvotes: 2

Kevin
Kevin

Reputation: 41893

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;

Upvotes: 1

Related Questions