Help with function urlencode [closed]

0

How can I make this function besides putting + in the spaces put -, and that it changes the %7C by | would anyone know? only in url

Below the Model of how it is:

I would like the url to be something like:

Marriage-joao - + - Maria- | -Assessment

    
asked by anonymous 27.05.2016 / 02:39

1 answer

2

I do not recommend you do this because urlencode is done to ensure interoperability between the browser and the application, and correct decoding depends on the characters being properly encoded (otherwise, the urlencode ).

Anyway, this will only cause problems, and when retrieving the value by the PHP application, will not be returned what you are expecting .


But if that's what you want, here is a way to change the URL:

$url = str_replace( '%7C', '|', $url );
$url = str_replace( '+', '-', $url );
$url = str_replace( '%2B', '+', $url );

See the result running as per IDEONE

Alternative syntax:

$de   = array( '%7C', '+', '%2B' ); // acrescente todos os
$para = array( '|'  , '-', '+'   ); // pares que quiser trocar
$url = str_replace( $de, $para, $url );

Function:

function leonardo_encode( $url ) {
   $de   = array( '%7C', '+', '%2B' );
   $para = array( '|'  , '-', '+'   );

   return str_replace( $de, $para, urlencode( $url ) );
}


rawurlencode vs urlencode

Note that for what you're doing, rawurlencode would be more appropriate than urlencode .

  • urlencode is usually used when you are dealing with query string , or post values:

    http://example.com/recurso?nome=dados+codificados
                                    ^^^^^^^^^^^^^^^^^
    
  • rawurlencode is usually used when you are messing with the path / resource name of the URL:

    http://example.com/recurso/dados%20codificados
                               ^^^^^^^^^^^^^^^^^^^
    
27.05.2016 / 02:46