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? 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; ?> 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]*) 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 "-". 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 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 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
Archived
This topic is now archived and is closed to further replies.