TonyLock Posted June 11, 2010 Share Posted June 11, 2010 Can someone help me with PHP RegEx? I'd like to be able to remove any characters that are not apart of this list: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789;,.!&?@#$%^(){}[]-+/\=*'" Here's what I'm using: $Text = 'Testing ? ? : $ ? ?` < > : 123'; $Text = preg_replace('/[^a-zA-Z0-9]+ ;,.!&?@\'\"\#\$\\\%\(\)\{\}\[\]\-\+;', '', $Text); So, echoing $Text should only display the following: Testing 123 Link to comment Share on other sites More sharing options...
0 bolerodan Posted June 11, 2010 Share Posted June 11, 2010 Hmm perhaps try ? $Text = preg_replace('/([^\w \'\"\#\$\\\%\(\)\{\}\[\]\-\+]+)/', '', $Text); I get the output Testing $ 123 You mention you should get Testing 123.. but isnt the $ part of what you want to keep (as I have the $ kept in my final result) Link to comment Share on other sites More sharing options...
0 Xoligy Posted June 11, 2010 Share Posted June 11, 2010 Hmm perhaps try ? $Text = preg_replace('/([^\w \'\"\#\$\\\%\(\)\{\}\[\]\-\+]+)/', '', $Text); I get the output Testing $ 123 You mention you should get Testing 123.. but isnt the $ part of what you want to keep (as I have the $ kept in my final result) out of his code $Text = 'Testing ? ? : $ ? ?` < > : 123'; he just wants to return "Testing 123" so symbols should be removed not kept =) Link to comment Share on other sites More sharing options...
0 sweetsam Posted June 11, 2010 Share Posted June 11, 2010 out of his code $Text = 'Testing ? ? : $ ? ?` < > : 123'; he just wants to return "Testing 123" so symbols should be removed not kept =) Yes but he also says... I'd like to be able to remove any characters that are not apart of this list: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789;,.!&?@#$%^(){}[]-+/\=*'" As you can see $ is part of what he wants to keep. Link to comment Share on other sites More sharing options...
0 bolerodan Posted June 11, 2010 Share Posted June 11, 2010 Yes but he also says... As you can see $ is part of what he wants to keep. Exactly, and according to my results, thats what should appear - based on his regular expression. also I just noticed in my regular expression I didnt add every symbol you want to keep, example the @ symbol I forgot to put in my regular expression, but those can be re-added in to your liking. Link to comment Share on other sites More sharing options...
Question
TonyLock
Can someone help me with PHP RegEx?
I'd like to be able to remove any characters that are not apart of this list:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789;,.!&?@#$%^(){}[]-+/\=*'"
Here's what I'm using:
$Text = 'Testing ? ? : $ ? ?` < > : 123'; $Text = preg_replace('/[^a-zA-Z0-9]+ ;,.!&?@\'\"\#\$\\\%\(\)\{\}\[\]\-\+;', '', $Text);So, echoing $Text should only display the following:
Testing 123
Link to comment
Share on other sites
4 answers to this question
Recommended Posts