Help with urlencode function

Asked

Viewed 269 times

0

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

Below the Model of how this: inserir a descrição da imagem aqui

would like that in the url something like:

Wedding-Joao-+-Maria-|-Essay

  • I’m wearing it, but it brings me this way to url like img and I did not want so would like something as specified below img

  • 4

    In my understanding, the question is out of scope for asking for a technically unviable solution, and that is not able to work in practice, nor to be taken advantage of by another user.

1 answer

2

I don’t recommend you do this, for the urlencode is made to ensure interoperability between the browser and the application, and the correct decoding depends on the characters being properly encoded (otherwise neither would be necessary urlencode).

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


But if that’s what you want, follow 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 requested on 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

Important to consider that for what you are doing, the rawurlencode would be more appropriate than the urlencode.

  • urlencode usually uses it when you’re messing with the query string, or post values:

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

    http://example.com/recurso/dados%20codificados
                               ^^^^^^^^^^^^^^^^^^^
    
  • type so there is no way I can change inside the function that? only the space that is %20 trade them in for - and not as a standard +, 'cause I’m gonna need the signal from +

  • No, the function serves to do the correct encoding. Note that you can change whatever you want, just put %20 on the left, and '-' on the right str_replace. Nothing prevents you from creating a function leonardo_encode and use the above code within its function. Note that the + is being preserved in the 3rd line if you view the test in the IDEONE will see that I did exactly what you asked in the question.

  • yes I realized I’m trying to make the run for the exchange

  • The biggest problem will start when you are going to recover this data by PHP

Browser other questions tagged

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