monkeytooth Posted January 14, 2010 Share Posted January 14, 2010 What I have here is what I am thinking is something to strip out all bad characters from a given string. What I am trying to do is strip out everything leaving only the letters and words and safe char's for output to use in a URL. $array_2strip = array("'", '"', ":", ";", "&", "\n", "\t", "\r", "%20", "<", ">", "?", "@", "#", "$", "^", "*", "(", ")", "=", "+", "~", "`", "|", "..", "...", " "); $booktitlelink = str_replace($array_2strip, '-', $booktitle); echo $booktitlelink ."<br />"; $array_2strip2 = array("--", "---"); $booktitlelink2 = str_replace($array_2strip2, '-', $booktitlelink); echo $booktitlelink2 ."<br />"; Outputs: Starting out With Java: From Control.. - With CD Starting-out-With-Java--From-Control----With-CD Starting-out-With-Java-From-Control--With-CD any ideas on how I can improve on the above? I am trying to mimic wordpress's SEO URL's main issue is the DB I am working with is a wreck and entirely tooo large to go through and edit manually, so I have to filter the outputs like this Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/ Share on other sites More sharing options...
trq Posted January 14, 2010 Share Posted January 14, 2010 Seems you would almost be better of using preg_replace to only allow a white list of chars. Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-994739 Share on other sites More sharing options...
monkeytooth Posted January 14, 2010 Author Share Posted January 14, 2010 other than dashes, and a-z 0-9, is there any other char's you think I can allow in a string thats being passed through a URL? Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-994752 Share on other sites More sharing options...
trq Posted January 14, 2010 Share Posted January 14, 2010 Probably only underscores, even then, I would likely use dashes instead. Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-994760 Share on other sites More sharing options...
monkeytooth Posted January 14, 2010 Author Share Posted January 14, 2010 Yea, I was reading up on SEO linking, and even a majority of sites recommend try keeping it to just - if at all possible. So, what would I do with preg_match? can preg_match do in string replacements? cause I am generally creating these on the fly, from stored records in a database. Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995012 Share on other sites More sharing options...
laffin Posted January 14, 2010 Share Posted January 14, 2010 not tested $seo=preg_replace('/([^a-zA-Z0-9])/','-',$string Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995021 Share on other sites More sharing options...
monkeytooth Posted January 14, 2010 Author Share Posted January 14, 2010 So if that doesn't work, its just all about the regex, I didn't think that worked with preg_ match/replace. But seeing as it does, if that doesnt work 100% im sure if i play with regex a bit i can come up with something to work properly enough. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995036 Share on other sites More sharing options...
laffin Posted January 14, 2010 Share Posted January 14, 2010 the pattern string is pretty simple ( ) - grab this as a variable [ ] - charaters in range ^ - negate (characters not in this range) a-zA-Z0-9- - alpha-numerics as well as - so it should grab everything else, and change it to '-' Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995072 Share on other sites More sharing options...
monkeytooth Posted January 14, 2010 Author Share Posted January 14, 2010 Thank you, that is one of my pitfalls with regex, I am not to clear on how it works, that last post actually helps me better understand it if at the least just enough to dabble and better grasp it, as I use regex alot, but usually preformated strings I have to dig for. Hey side question, do you know of either any good books or online tutorials on regex, that kind of dumb it down for people new to it? Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995115 Share on other sites More sharing options...
simshaun Posted January 14, 2010 Share Posted January 14, 2010 One of the best investments I've made besides in my IDE is RegexBuddy. It will help you learn regex and is great for building & testing. Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995120 Share on other sites More sharing options...
laffin Posted January 14, 2010 Share Posted January 14, 2010 a nice tutorial, for the basics a online regex tester one of my favorite tools for windows is expresso Quote Link to comment https://forums.phpfreaks.com/topic/188430-str_replace-looking-for-a-better-method-or-ideas-on-how-to-improve/#findComment-995245 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.