PHP remove a URI snippet with regular expression

Asked

Viewed 331 times

2

I have the following Uri

$uri = "http://www.meudominio.com.br/pagina.php?campo=teste&ordem=2&cor=azul"

how to remove the "ordem=2" of the above url with regular expression, knowing that the value of the order, time can be 2, 3, 4 and etc?

http://www.meudominio.com.br/pagina.php?campo=teste&cor=azul

could try like this?

$arr_nova_uri = explode("ordem=", $uri);
$nova_uri = $arr_nova_uri[0].substr($arr_nova_uri[1], 2);
  • 3

    possible duplicate of http://answall.com/a/123638/4793

3 answers

7

Regular expression:

/(?:(\?)|&)ordem=\d+(?(1)(?:&|$)|(&|$))/

Substituting:

$1$2

Code:

$uri   = 'http://www.meudominio.com.br/pagina.php?campo=teste&ordem=2&cor=azul';
$re    = '/(?:(\?)|&)ordem=\d+(?(1)(?:&|$)|(&|$))/';
$subst = '$1$2';

$nova_uri = preg_replace($re, $subst, $uri);

echo $nova_uri;

Upshot:

http://www.meudominio.com.br/pagina.php?campo=teste&cor=azul
  • 1

    Closed!... perfect..

  • ah a small gaf if it is the last parameter : Behold, but nothing that interferes much. Unless you take all the parameters by array.

  • Suggestion, don’t worry about what to see later, example.

  • @Guilhermelautert Thank you for the suggestion. & is valid at the end. But I edited the answer to consider it. Your example has a problem in the latter case. It is not being validated that it is only numbers and replaces the ? for &.

  • If you want to reverse the order of the parameters, simply remove the &order=N $re = '/&order= d+/'; $str = 'http://www.meudominio.com.br/pagina.php? field=teste&order=2&color=blue'; $result = preg_replace($re,'', $str);

  • @Marcosxavier Try this other example: http://www.meudominio.com.br/pagina.php?ordem=2&campo=teste

  • Simple. https://regex101.com/r/YIR8IP/1

  • @Marcosxavier It’s not that simple: pagina.php?desordem=2FFF&campo=teste

  • @Mariano is very good, if you want you can reduce, I had never used the (?(R)), very interesting.

  • @Guilherme Thanks! Yes. But I wanted to validate this case.

  • @Mariano got it :D

  • @Mariano Take a look at the post http://answall.com/questions/159428/php-remover-um-trecho-da-uri-com-regularexpression#159549

  • @If your question has already been resolved, it would be nice if you could review your questions and see if any answers deserve a vote and acceptance. Read How and why to accept an answer? and click the button .

Show 8 more comments

5

On my Github I created a gist with a function to do operations like this in a reusable way. Besides, I prefer not to use regular expression, as these usually cost more in terms of performance.

This function allows you to remove, replace or add parameters to a given url. If it already has Query String, it will be replaced or added.

Behold:

function url_replace_query($url, array $parameters)
{
    $parts = parse_url($url) + [
        'scheme' => 'http',
        'query'  => NULL,
        'path'   => NULL,
    ];
    if (! isset($parts['host'])) return false;
    parse_str($parts['query'], $query);
    $parameters += $query;
    $newQueryString = http_build_query($parameters);
    return $parts['scheme'] . '://' . $parts['host'] . $parts['path'] . '?' . $newQueryString;
}

You can use it like this:

$url_sem_ordem = url_replace_query("http://www.meudominio.com.br/pagina.php?campo=teste&ordem=2&cor=azul", ['ordem' => null])

Upshot:

'http://www.meudominio.com.br/pagina.php?campo=teste&cor=azul'
  • 1

    Interesting is, but I found too much Alaca for simple thing. + 1.

0

If the order parameter is alphanumeric you can use the following example.

Ps. Posted here for comments not to extend too much

Run in https://regex101.com/r/YIR8IP/2

$re = '/&?ordem=(\d+)?(\w+)?/';
$str = 'pagina.php?campo=teste&ordem=2&cor=azul
pagina.php?ordem=2&campo=teste
pagina.php?ordem=2FFF&campo=teste
pagina.php?ordem=FFF&campo=teste
 ';
$subst = '';
$result = preg_replace($re, $subst, $str);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.