[UPHPU] Regex help.
Kyle Waters
unum at unum5.org
Fri Jan 30 11:33:24 MST 2009
Ash wrote:
> I have a database that has addresses in it. The only characters
> allowed are A-z 0-9 and ,()':.
>
> The database also has other characters in it, but I want to delete all
> the characters that are not allowed (because they crash my program
> that prints the addresses to the printer.)
>
> It's just a text file, so I could run sed on it, but how do you test
> for just those characters?
>
So if I understand you have a text file and you want to remove all
characters that are not A-Z a-z ,()':.
I would do the following:
$contents=file_get_contents("file.txt");
preg_replace('[^A-Za-z\,\(\)\'\:\.]','',$contents);
file_put_contents("file.txt",$contents);
That will remove all other characters from the file. (Though you should
wait until someone else posts and fixes my regex).
Kyle
More information about the UPHPU
mailing list