ESeufert Posted September 11, 2007 Share Posted September 11, 2007 I'm working on a program that allows users to submit short articles. I'm going to mod_rewrite the URLs to make them friendly, and I need each article name to be unique so that access is simplified like this: Article Name: How to Tie Your Shoe Article URL: www.mysite.com/articles/how_to_tie_your_shoe I'll store the article name and the URL-friendly access name (how_to_tie_your_shoe) in a database. Is there a function or mod out there that will do this for me? I feel like writing this would be reinventing the wheel. I just need to basically strip out special characters and replace spaces with _, but I'm wondering if I'm forgetting anything else that would prevent a URL from rendering properly. The second part is making these unique: I need to check the database for pre-existing articles with the same submitted name and put together a unique identifier. I can easily append words onto the end of the article name based on some other criteria (like the article's author or when it was written), but again I have the feeling that this process has already been coded 1000 times by others. A google search, however, left me empty-handed. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/68870-making-an-article-name-url-friendly/ Share on other sites More sharing options...
Jessica Posted September 11, 2007 Share Posted September 11, 2007 You'll likely have to write it yourself if you haven't found anything searching google. There are plenty of code repository sites out there, but I have no idea if what you want is on one. If you don't want to write it, hire someone. Quote Link to comment https://forums.phpfreaks.com/topic/68870-making-an-article-name-url-friendly/#findComment-346128 Share on other sites More sharing options...
Daniel0 Posted September 11, 2007 Share Posted September 11, 2007 Is there a function or mod out there that will do this for me? I feel like writing this would be reinventing the wheel. I just need to basically strip out special characters and replace spaces with _, but I'm wondering if I'm forgetting anything else that would prevent a URL from rendering properly. <?php $string = "How to Tie Your Shoe"; echo strtolower(str_replace(' ','_',preg_replace('/[^a-zA-Z0-9 ]/', '', $string))); // output: how_to_tie_your_shoe ?> Quote Link to comment https://forums.phpfreaks.com/topic/68870-making-an-article-name-url-friendly/#findComment-346214 Share on other sites More sharing options...
feuerfalke Posted September 11, 2007 Share Posted September 11, 2007 You could also use the urlencode() and urldecode() functions: <?PHP $string = "How to Tie Your Shoe"; $encoded = urlencode($string); // Outputs "How+to+Tie+Your+Shoe" $decoded = urldecore($encoded); // Outputs "How to Tie Your Shoe" ?> Quote Link to comment https://forums.phpfreaks.com/topic/68870-making-an-article-name-url-friendly/#findComment-346238 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.