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.
views:
53answers:
4
+1
A:
It's as simple as this:
$tag = 'ruby on rails';
$newTag = str_replace(' ', '-', trim($tag));
Ben Rowe
2010-08-09 05:32:15
+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
2010-08-09 05:33:05
what kind of question do you think I'm asking?
helpME
2010-08-09 05:35:03
@helpMe, the kind in which the answer is different from the one Alan posted.
Yar
2010-08-09 05:55:26
+1 This is what I read the obtuse question as.
Andrew
2010-08-09 05:57:24
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
2010-08-09 14:51:48
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
2010-08-09 05:38:09
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
2010-08-09 06:11:20