Re: Replacing text with criteria

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jeff Lewis wrote:
I have been working on a script to parse a CSV file and in the process I
clean out a lot of the garbage not needed but I am a bit stumped on one
aspect of this file. It's a list of over 3000 contacts and I'm trying to
mask a lot of the information with *.
So as I loop through this list I am checking the array elements as if I find
a phone number (555) 555-1212 (I was searching for the (555)) I want to
convert it to (555) ***-****
For fields not containing an area code, I wanted any text to be replaced by
an * except for the first and last letter in the element.
I'm stumped and was hoping that this may be something horribly simple I'm
overlooking that someone can point out for me.


This will take a string and replace everything but the first and last characters.

$string = substr_replace($string, str_repeat("*", strlen($string)-2), 1, -1);

It seems like you want to do it based on letters though, so "Hello, World!" would become "H**********d!", not ""H***********!" (which the above would do). To do this you'll need to get the positions of the first and last letters in the string and use those instead of 1 and -1. Off the top of my head I don't recall any functions that can do this, strpos and strrpos don't take arrays as parameters. You may be better off with regular expressions for the actual replacement unless somebody knows of a function to get the positions.

BTW, in functions like substr, specifying "-1" for length means keep going until the second to last character.

Regards, Adam.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux