...

View Full Version : substr replace problem



Kal
03-27-2007, 04:38 PM
Hi guys

if i have a string say of 11 characters, how can i display only the last 3 but replace the first 8 with an asterix.

for example

original 12345678910

new ********910

thanks in advance

Krokador
03-27-2007, 05:29 PM
get the string lenght, and then put together the number of * you need and the rest of the string. Like:


$length = strlen($string); //get string lenght
$newstring = ""; //declare a new string
for($i=0; i<$length-3; $i++) //add the *'s depending on the number of characters you want to keep at the end
$newstring += "*";
$newstring += substr($string, $length-3); //add the end of the string to the new one
echo $newstring; //show the new string


I think that should help, I haven't tested so I might've gone wrong somewhere, but normally this should work.

rafiki
03-27-2007, 05:34 PM
believe you left out something
the $ before i<$length-3

for($i=0; $i<$length-3; $i++) //add the *'s depending on the number of characters you want to keep at the end

GJay
03-27-2007, 08:37 PM
$string='12345678';
echo str_repeat('*',8).substr($string,-4);

is a bit shorter... :)

marek_mar
03-27-2007, 09:48 PM
$str = 'your string';
$length = strlen($str);
str_pad(substr($str, -4), $length, '*', STR_PAD_LEFT);

Kal
03-28-2007, 08:36 AM
thanks guys for your help all sorted.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum