-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;
asked Jul 9, 2015 at 8:17
10
  • 1
    No for loop, no strpos, just str_replace() ... ?! Commented Jul 9, 2015 at 8:18
  • 2
    Your for loop sets $spaces to 0 so there is nothing to replace,maybe show what you have in $_GET Commented Jul 9, 2015 at 8:19
  • 1
    stackoverflow.com/questions/2109325/… Commented Jul 9, 2015 at 8:21
  • can you please show $codes value? Commented Jul 9, 2015 at 9:46
  • @anantkumarsingh Codes contains whatever you put into form textarea. Commented Jul 9, 2015 at 9:48

3 Answers 3

5
$string = str_replace(' ', '', $string);
answered Jul 9, 2015 at 8:19
Sign up to request clarification or add additional context in comments.

4 Comments

it seems that your answer is much similar to Hanky.
He posted it while I was writing it, I did not copy. He posted it only 30 secs before I did
Thank you, he's 28k5 fame, i only have 105, I deserve more upvote than him xD (just kidding)
I just have edited my post, but it still doesnt seem to work at all
4
$text=str_replace(" ","",$text);

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

answered Jul 9, 2015 at 8:19

1 Comment

I just have edited my post, but it still doesnt seem to work at all
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

answered Jul 9, 2015 at 8:23

2 Comments

I just have edited my post, but it still doesnt seem to work at all
@LetMeLearn123 can you show the output of var_dump($_GET['codes']);?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.