Jump to content

strip everything but alphanumeric, _ - ? ' "


Recommended Posts

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 );

Link to comment
https://forums.phpfreaks.com/topic/267729-strip-everything-but-alphanumeric-_/
Share on other sites

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?'"-]

I can't imagine you really wanting this:

 

"httpfeedsfeedburnercomtechcrunchstartups?formatxml"

 

Try re-reading the first post. :shy:

 

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.

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? :shy:

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 );

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.