-1

I have a form where people enter their multiple codes. Now, I would like to delete the spaces between these codes. However, my code doesnt seem to work. Any ideas?

$codes = $_GET['codes'];
$spaces = strpos($codes, " ");

for($spaces; $spaces=0; $spaces--){
str_replace(" ", "", $codes);
echo $codes;
}

EDIT: I just have tried something else but it still doesnt work at all. I mean the echo gives me the original string every single time.

$codes = $_GET['codes'];
$cleancodes = str_replace(" ", "", $codes);
$cleancodes = trim(preg_replace('/\s\s+/', ' ', $cleancodes));
echo "<br / >" . $cleancodes;
LetMeLearn123
  • 61
  • 1
  • 2
  • 9

3 Answers3

5
$string = str_replace(' ', '', $string);
Hearner
  • 2,711
  • 3
  • 17
  • 34
4
$text=str_replace(" ","",$text);

But doing that for code? Bound to break (if you meant program code)!

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
2

Use str_replace():-

<?php
$_GET['codes'] = "abc def ghi jkl mno pqr stu vwx yz ";
$codes = $_GET['codes'];
$codes = str_replace(" ","",$codes);
echo $codes;

Output:-https://eval.in/395364

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98