dadamssg87 Posted August 29, 2012 Share Posted August 29, 2012 i'm trying to figure out how to write a function that strips anything other than: 1. alpha-numeric characters 2. underscore _ 3. hyphen - 4. question mark ? 5. apostrophe ' 6. quotation mark " I've got the alphanumeric part down, i just don't know how to add those other characters str_replace("[^A-Za-z0-9]", "", $string_to_be_stripped ); Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/ Share on other sites More sharing options...
requinix Posted August 29, 2012 Share Posted August 29, 2012 1. All of them except the hyphen can go in directly without anything special (except having to escape the quote for PHP). The hyphen needs to go in a place where it can't act as a range separator; easiest way is to put it at the beginning or end of the set. 2. \w includes letters, numbers, and underscores. [^\w?'"-] Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373486 Share on other sites More sharing options...
dadamssg87 Posted August 29, 2012 Author Share Posted August 29, 2012 sweeeet. What if i wanted to add commas and exclamation points? Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373487 Share on other sites More sharing options...
requinix Posted August 29, 2012 Share Posted August 29, 2012 Sure: they're not special either. Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373494 Share on other sites More sharing options...
dadamssg87 Posted August 29, 2012 Author Share Posted August 29, 2012 I think i'm missing something. The following string is exactly the same after it goes through str_replace...? $str = 'http://feeds.feedburner.com/techcrunch/startups?format=xml'; echo str_replace("[^\w?',!\"-]", "", $str ); Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373528 Share on other sites More sharing options...
salathe Posted August 29, 2012 Share Posted August 29, 2012 str_replace...? str_replace() does not replace based on a regular expression, you're looking for preg_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373530 Share on other sites More sharing options...
Jessica Posted August 29, 2012 Share Posted August 29, 2012 Are you just trying to get the URL without the query string? Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373549 Share on other sites More sharing options...
.josh Posted August 29, 2012 Share Posted August 29, 2012 yeah...explain what you actually want here, what your goal is... because I can't imagine you really wanting this: "httpfeedsfeedburnercomtechcrunchstartups?formatxml" Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373579 Share on other sites More sharing options...
salathe Posted August 29, 2012 Share Posted August 29, 2012 I can't imagine you really wanting this: "httpfeedsfeedburnercomtechcrunchstartups?formatxml" Try re-reading the first post. Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373606 Share on other sites More sharing options...
Jessica Posted August 29, 2012 Share Posted August 29, 2012 I can't imagine you really wanting this: "httpfeedsfeedburnercomtechcrunchstartups?formatxml" Try re-reading the first post. And? I read it. It says he wants to strip anything other than alphanumeric and 5 special characters, which is what josh left in the string. Alphanumeric and those characters. Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373607 Share on other sites More sharing options...
.josh Posted August 29, 2012 Share Posted August 29, 2012 sally is just looking for a thinly-veiled excuse to flirt with me; see how he blushes. Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373614 Share on other sites More sharing options...
salathe Posted August 29, 2012 Share Posted August 29, 2012 And? I read it. It says he wants to strip anything other than alphanumeric and 5 special characters, which is what josh left in the string. Alphanumeric and those characters. Good work on reading it, really. I guess you totally failed to see my point, which was that .josh "couldn't imagine" something that was very clearly asked for in the first post. Did you think I was trying to say .josh said something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373691 Share on other sites More sharing options...
Jessica Posted August 29, 2012 Share Posted August 29, 2012 Well yeah, I did. I figured out of the number of people replying here, it was likely that someone was getting it wrong. *shrug* Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373693 Share on other sites More sharing options...
dadamssg87 Posted August 29, 2012 Author Share Posted August 29, 2012 Thanks for the responses! buuuut it's still not doing what i need to be doing. The provided $str being a url is just an example string because it contains characters i'm wanting to strip. I won't strictly be using this on urls. $str = 'http://feeds.feedburner.com/techcrunch/startups?format=xml'; echo preg_replace("[^\w?',!\"-]", "", $str ); Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373714 Share on other sites More sharing options...
Christian F. Posted August 30, 2012 Share Posted August 30, 2012 You're a bit confused about the Regular Expression syntax, as you're missing the delimiters. Add a slash at the start and end of the pattern, and it should work. Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373726 Share on other sites More sharing options...
.josh Posted August 30, 2012 Share Posted August 30, 2012 I just assumed that his "example" was an example of what he's wanting the regex to act on, which didn't seem to have any practical or apparent purpose, despite it *technically* being what he asked for. In my experience, when someone is asking for help trying to do something, they often end up asking for something other than what they *really* want, due to being unclear about the situation in the first place! But I guess you know what they say about assuming... dadamssg87, in the future, if you want avoid having your time wasted by rebels like me who refuse to follow the "Just gimme what I asked for, don't question why" philosophy, I suggest giving more relevant examples. Quote Link to comment https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/#findComment-1373729 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.