[UPHPU] Regex help.
Ash
ashovi at qwest.net
Fri Jan 30 13:44:45 MST 2009
Kyle Waters wrote:
> 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
>
Thanks for the point in the right direction at least. I can't get it to
work yet, but I'm reading up on preg_replace and am trying a bunch of
different things. Here's my CLI script as it stands right now (I took
out everything except A-Z to make sure there wasn't something weird with
the other characters going on.):
#!/usr/bin/php -q
<?PHP
$contents=file_get_contents("file.txt");
preg_replace('^A-Za-z','',$contents);
file_put_contents("file.txt",$contents);
?>
The timestamp on file.txt changes, but the text in it stays the same.
Ash
More information about the UPHPU
mailing list