Remove space from a php string

Asked

Viewed 325 times

1

I have a problem with an appointment SQL on the basis of a array.

I am concatenating the values received from a text-area, by sql with a IN, follows sql example:

select * from jupiter where imei in ('352997100159046 ','351758105120669 ','357770077861257 ','358492061345614')

And based on this query return the data to a table. It happens if they repair has a space ' ' after numbering, and for some reason does not return the data in PHP (In the database ide returns normally).

I tried in several ways to remove this space, follows examples:

$final = str_replace(' ', '', $manipulado2);

$str = trim(preg_replace('/\s\s+/', ' ', $final));

I also created an array with characters that I do not want and removed them but also without success:

$caracteres             = array(" ","a", "b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",".",";"," ", "^", "~","-","!","@","#","$","%","¨","&","*","(",")","=","+","\r\n");

But in no way I can remove these spaces from my query.

These data are wrong in a vector, follows explanatory image:

inserir a descrição da imagem aqui

As you can see at last it does not contain the space and it returns the correct search.

It follows how the concatetion is being made (in a very primitive way but it is how I managed to do):

I receive via post the data of a textarea that is separated by enters, and replace the line break by ','.

$resultado              = str_replace("\n","','", $_POST['imeis']);

Here I call the function that this row above is and separate into the comma and put that into an array.

$array_informado = explode(',', trata_imei());

Then I throw it to one foreach to show off in my table:

print_r($array_informado);
foreach ($array_informado as $rows){

This print_r, is what printa in the image above, it already brings the information with the spaces!!

Does anyone know why these spaces are not being erased from my string?

Thank you!

  • The problem could be in the concatenation. You should show in the question how this is being done (put an example of the textarea with the data -- if you have line breaks etc. -- and the code that concatenates).

  • I’ll put it right away!

  • @Sam Edited!!!

  • a demon won’t fix?

  • I tried with Trim, it doesn’t solve either :\

  • I have the impression that it identifies as if it were a different character, or so understand that the character is the full number + the space, and not just the space...

  • In the preg_replace, a regex /\s\s+/ requires at least 2 spaces (one \s corresponds to one space, the other \s+ is "one or more spaces", that is, it has to have at least 2, so it does not replace anything). Maybe only do preg_replace('/\s+$/', '', $final) already solved - although the trim should remove anyway. I don’t know, the trim only removes a few characters (see documentation) but Unicode defines other "space" characters, maybe it is one of those: http://www.fileformat.info/info/unicode/category/Zs/list.htm

  • I found something similar link

  • I believe this is the way... I tried it this way $str = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $final); but without success. the return was '352997100159046 ','357770077861257 ','351758105120669'

  • I’ll give an Edit in the post, please see

Show 5 more comments

1 answer

1


Do two Places, where the first one removes the \r (carriage return, which is what is generating the extra space), and another replacing the line break \n for ','.

Example:

$resultado = str_replace("\n", "','", str_replace("\r","", $_POST['imeis']));

Then just adapt in the function where you are treating the result to generate the string you want.

  • 1

    It worked, you’re the man! Thank you!!!

Browser other questions tagged

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