![]() |
regular expression help
I want to clean a database of words that have bad letters in them. I know how to remove bad characters, but how can I remove the word along with it?
"The qu&&ick brown fox ju&&&mped over the lazy dog." assuming & is a bad character, how can I end up with "The brown fox over the lazy dog." Basically, anything word that doesn't have an alpha-numeric or [.,<>?~@#$%^&*()] I want to remove the word. Single preg_replace expression?? |
is it wordpress? or just another site db, if it was wordpress, you could use the search and replace plugin.
|
bascially you want to execute
Code:
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]'); http://sewmyheadon.com/2009/mysql-search-replace-tool/ |
Quote:
|
Quote:
To remove words that have the "bad" character: \w is the class of word characters. You're looking for a string containing at least one "bad character" and optionally some word characters. "Words", as you define them, are strings of word characters and &, which is represented as \w|& . So your assuming & is the bad character, the regular expression is: (\w|&)*&(\w|&)* preg_replace('/(\w|&)*&(\w|&)*/', "", $subject); Quote:
So you're looking for: \s[^.,<>?~@#$%^&*()]+\s and replacing it with a single space delimiter like this: preg_replace('/\s[^.,<>?~@#$%^&*()]+\s/', " ", $subject); |
Quote:
DAMN that was quick, well done! :thumbsup |
Yes, thanks - you are correct in my typo. And thanks for the answer - coding now :-)
|
Quote:
:) This stuff was hard in 1997 when we were trying to get referer based .htaccess right. We had to watch out for things like goodguy.com.hacker.com I've had a bit of practice since then. Camperjohn, what I posted is only known to be correct, not tested. |
I would just write a quick script to do that, fetch text from db, split into words, check each word, unsplit, save it...
a bit slower and less efficient, but pretty hard to fuck up... on the other hand with one regexp command, one wrong character and your whole db could get fucked up... |
Quote:
|
Quote:
yes definitely either way one would first to a database dump or CREATE TABLE backup SELECT * FROM thetable. Messing up is okay, it happens. Breaking things is not. You'd want to backup either way because for example even of you her the word delete perfect, join is not the inverse of split, so data could be lost by splitting and joining. |
All times are GMT -7. The time now is 05:42 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123