1

I have a form that when submitted loads in the post id into a variable. However some of the options in the form have spaces between the words. For example

one option is "Kanye West" now it loads into the variable the words Kanye and West with a space inbetween.

I need to be able to add a + symbol between these two words instead of a space. So it would be Kanye+West. How would i go about doing this?

1
  • I got here by searching for Kanye West. Thumbs Up if you did. Commented Aug 2, 2011 at 12:50

7 Answers 7

4

Simple case: strtr()

$str = strtr($str, ' ', '+');

Generic: urlencode()

$str = urlencode($str);

Unless I'm misunderstanding the question.

Sign up to request clarification or add additional context in comments.

Comments

3

The urlencode function was designed exactly for this purpose. It converts all special characters (including space) to their url-safe equivalents (e.g. +).

3 Comments

how would i do this, on this code - $artistResult = $_POST['select']; (select for example would be Kanye West)
$artistResult = urlencode($_POST['select']); (Not too tough, eh?)
@DIM3NSION That would be $artistResult = urlencode($_POST['select']);, with $_POST['select'] = 'Kanye West' giving $artistResult = 'Kanye+West'. (Edit: Too slow ... again.)
2

You can use strtr():

$str = strtr(trim($str), ' ', '+');

If you want to replace several consecutive white space characters with one +, use preg_replace:

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

Comments

1

Try str_replace(' ', '+', $originalString) on the PHP side before output.

Comments

1

You can use:

$my_new_string = str_replace(" ", "+", $my_oldstring)

Comments

0
$str = 'Kanye West';
$str = str_replace(' ', '+', $str);

Comments

0

you want to use str_replace

$var; // from your post $var = str_replace(" ", "+",$var);

Comments

Your Answer

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.