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

Asked

Viewed 263 times

-1

2 answers

2


If you’re in one <input> only the message just use rawurlencode, so 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 you are 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>

1

Try to use str_replace:

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

Browser other questions tagged

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