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
LetMeLearn123
611 gold badge2 silver badges10 bronze badges
3 Answers 3
$string = str_replace(' ', '', $string);
answered Jul 9, 2015 at 8:19
Hearner
2,7073 gold badges19 silver badges34 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Chiragkumar Thakar
it seems that your answer is much similar to Hanky.
Hearner
He posted it while I was writing it, I did not copy. He posted it only 30 secs before I did
Hearner
Thank you, he's 28k5 fame, i only have 105, I deserve more upvote than him xD (just kidding)
LetMeLearn123
I just have edited my post, but it still doesnt seem to work at all
$text=str_replace(" ","",$text);
But doing that for code? Bound to break (if you meant program code)!
answered Jul 9, 2015 at 8:19
Hanky Panky
46.9k9 gold badges76 silver badges95 bronze badges
1 Comment
LetMeLearn123
I just have edited my post, but it still doesnt seem to work at all
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
Death-is-the-real-truth
72.3k10 gold badges59 silver badges106 bronze badges
2 Comments
LetMeLearn123
I just have edited my post, but it still doesnt seem to work at all
Death-is-the-real-truth
@LetMeLearn123 can you show the output of
var_dump($_GET['codes']);?lang-php
str_replace()... ?!$codesvalue?