0

I'm need of a tiny help.I have a get statement on my process form like this

<?php echo $_GET['search']?>

Now how do i put "+" sign in between the spaces.Because what my current code does is just gets the data from the text of another form.

I would be glad if you could help.

Thanking You,

0

6 Answers 6

6
<?php echo str_replace(' ', ' + ', $_GET['search']) ?>
0
1

You can use the str_replace function this way:

$new_text = str_replace(' ', ' + ', $_GET['search']);
0
1

What about input that looks like

hello  world

(two spaces between the words)? Do you want to get hello + + world ?

If not, I suggest to remove the white spaces from the beginning and the end of the string first and use a regular expression to substitute several following white spaces with one +:

preg_replace('#\s+#', ' + ', trim($_GET['search']))
0

This will allow you to modify a regex to match more than just spaces.

function insertPlusSign($str)
{
    $func = create_function('$c', 'return $c . '+' . $c;');
    return preg_replace_callback('/ /', $func, $str);
}
0
0

You could also use urlencode, which also encodes the string to be used in a query part of a URL

echo urlencode('word1 word2 word3 word4 wiérdText@');
// Output: word1+word2+word3+word4+wi%E9rdText%40
1
-3

$newText=str_replace(' ', ' + ', $_GET['search']);

str_replace will replace the text as desired and save the new updated string in $newText

1
  • 1
    same answer already given TWICE :> Sep 23, 2010 at 17:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.