Remove white space php

Asked

Viewed 1,083 times

0

When typing the values (one per line), for example:

    valor1
    valor2
    valor3
    valor4

It will return: value1,value2,Valor3,value4

php input.

    <html>
    <body> 
    <form action="junta.php" method="post">
    <textarea rows="10" cols="50" name="valores">
    </textarea><br>
    <input type="submit" name="enviar" value="Pronto" />
    </form>
    </body> 
    </html>

php joint.

    <?php
    $valores = $_POST['valores'];
    $quebra = explode("\n", $valores);
    $junta = implode(',', $quebra);
    $valores = preg_replace("/\s+/", "", $junta);
    echo $valores;
    ?>
  • What input are you putting in? If it’s the four values, one in each row, your code gets even more confusing. Do you break the string nos n, put it all together, remove all blank spaces and in the end replace the spaces by comma? At this point there will be no spaces to be replaced by commas and their result is valor1valor2valor3valor4 (link). Are you sure this is the code that’s running? If so, please do a [mcve] demonstrating the problem - it could be repl.it or ideone.com.

1 answer

1


Well, if you want to remove ALL whitespace, you can use a regular expression.

$valores = preg_replace("/\s+/", "", $_POST["valores"]);
  • That’s exactly what I needed. Thank you.

Browser other questions tagged

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