tags:

views:

53

answers:

4

I was wondering if some one enters a tag like ruby on rails is there a way I can add a hyphen to the white spaces between the words for example ruby-on-rails using PHP.

+1  A: 

It's as simple as this:

$tag = 'ruby on rails';
$newTag = str_replace(' ', '-', trim($tag));
Ben Rowe
+1  A: 

I suspect you're asking a different question, but lacking more context ...

$string = 'ruby on rails';
$string_with_dashes = str_replace(' ','-',$string);

should get you where you want to go.

Alan Storm
what kind of question do you think I'm asking?
helpME
@helpMe, the kind in which the answer is different from the one Alan posted.
Yar
+1 This is what I read the obtuse question as.
Andrew
It sounded like you might be using some third party tagging library and you might want to have it add the hyphens automatically.
Alan Storm
A: 
$str = 'ruby on rails'; // your entered tag
$myTag = trim($str); // remove extra spaces from beginning and end     
$hyphenTag = str_replace( ' ', '-', $myTag ); // place '-' between words
echo $hyphenTag; // print result
NAVEED
A: 

Let me guess

$s = strtolower(trim($s));
$s = str_replace(" ","-",$s);
$s = preg_replace('![^a-z0-9-]!',"",$s);
$s = preg_replace('!\-+!',"-",$s);
Col. Shrapnel