View Full Version : substr replace problem
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.
thanks in advance
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.
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
is a bit shorter... :)
03-27-2007, 09:48 PM
$str = 'your string';
$length = strlen($str);
str_pad(substr($str, -4), $length, '*', STR_PAD_LEFT);
thanks guys for your help all sorted.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.