kks_krishna Posted July 10, 2007 Share Posted July 10, 2007 HI, I want to create urls dynamically when i am inserting the new data to database. ForExample, If the story title is : What is new in Site? It should be converted as : what-is-new-in-site.html It shouldn't allow any special characters in the URL. How can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/59228-need-help-for-creating-url-using-php-reg-exp/ Share on other sites More sharing options...
chocopi Posted July 10, 2007 Share Posted July 10, 2007 well to convert the spaces you would so something like <?php $str = "What is new in Site?"; $new_str = str_replace(" ","-",$str); echo $str; echo $new_str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/59228-need-help-for-creating-url-using-php-reg-exp/#findComment-294214 Share on other sites More sharing options...
Yesideez Posted July 10, 2007 Share Posted July 10, 2007 I'd make one small change to that: <?php $str="What is new in Site?"; $new_str=strtolower(str_replace(" ","-",$str)); echo $str; echo $new_str; ?> Best to keep URLs lower case. You'd also want to remove all non allowed characters like the ? as that wouldn't be allowed in the URL. Not exactly sure how that'd be done although I have written this regex string to grab everything except the ? ([ a-zA-Z]*) Quote Link to comment https://forums.phpfreaks.com/topic/59228-need-help-for-creating-url-using-php-reg-exp/#findComment-294219 Share on other sites More sharing options...
kks_krishna Posted July 10, 2007 Author Share Posted July 10, 2007 I don't want to show any special characters other than "-". Quote Link to comment https://forums.phpfreaks.com/topic/59228-need-help-for-creating-url-using-php-reg-exp/#findComment-294241 Share on other sites More sharing options...
chocopi Posted July 10, 2007 Share Posted July 10, 2007 I dont quite understand. Do you want an error if anything but alpha-numeric and - come up OR Do you want it just to remove them so HE-LLO$^ M"Y n@A~me IS C-h(oc*o&pi would become HE-LLO MY nAme IS C-hocopi ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/59228-need-help-for-creating-url-using-php-reg-exp/#findComment-294394 Share on other sites More sharing options...
kks_krishna Posted July 10, 2007 Author Share Posted July 10, 2007 Do you want it just to remove them so HE-LLO$^ M"Y n@A~me IS C-h(oc*o&pi would become HE-LLO MY nAme IS C-hocopi Yes, I want to remove all..but the spaces will be replaed with "-" and also there should not more than one "-" continuosly like "---". It should be trimed to "-" Thanks, krishna Quote Link to comment https://forums.phpfreaks.com/topic/59228-need-help-for-creating-url-using-php-reg-exp/#findComment-294403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.