How to transform the space of an input text to% 20?

-1

How to transform the space of an input text to% 20? I would like to change the text in question to be transported in a url

I am generating a url so that the site information is included at the beginning of the conversation ("text = your% 20message") example: link

    
asked by anonymous 24.03.2018 / 00:55

2 answers

2

If you are in a <input> only the message just use rawurlencode , as in PHP:

<?php
if (!empty($_POST['message'])) {
    $message = rawurlencode($_POST['message']);

    $url = 'https://api.whatsapp.com/send?phone=seunumerodetelefone&text=' . $message;

    var_dump($url); //Somente para testar a URL
}

Assuming the form is like this

<form action="pagina.php" method="POST">
<input type="text" name="message">
<button>Enviar</button>
</form>

If it's JavaScript you can use encodeURIComponent :

var message = document.querySelector("#message"),
    testBtn = document.querySelector("#testar");

testBtn.onclick = function () {
    var url = 'https://api.whatsapp.com/send?phone=seunumerodetelefone&text=';
    var urlmessage = url + encodeURIComponent(message.value);
    
    console.log(urlmessage);
};
Digite algo: <input type="text" id="message" value=""><br>
<button id="testar">Enviar</button>
    
24.03.2018 / 04:29
1

Try using str_replace:

str_replace(' ', '%20', 'https://api.whatsapp.com/send?phone=seunumerodetelefone&text=sua mensagem')

    
24.03.2018 / 02:48