I want to display first and last 5 character from following string.
APA91bGjUqf8O8yoajdN9BNf2Hs1iVm3VL37X7rn1_XiU1bcKOWVyaIYYusL8f5BCgzSw1HhaPbgntuYHFCR0VWrqGb59nDHMVfgJ-zK0SA0SWw0dvdMEB8AwI-Ltn56aBb0L-0tP_pkqZIbPltb71-u6inawaPfQw
I want following output:
APA91...aPfQw
How to solve?
Use use this code. But you can do with preg_match
too
$input = 'your_string';
$output = substr($input, 0, 5) . substr($input, -5);
echo substr('APA91bGjUqf8O8yoajdN9BNf2Hs1iVm3VL37X7rn1_XiU1bcKOWVyaIYYusL8f5BCgzSw1HhaPbgntuYHFCR0VWrqGb59nDHMVfgJ-zK0SA0SWw0dvdMEB8AwI-Ltn56aBb0L-0tP_pkqZIbPltb71-u6inawaPfQw', 0, 5) . '...' .substr('APA91bGjUqf8O8yoajdN9BNf2Hs1iVm3VL37X7rn1_XiU1bcKOWVyaIYYusL8f5BCgzSw1HhaPbgntuYHFCR0VWrqGb59nDHMVfgJ-zK0SA0SWw0dvdMEB8AwI-Ltn56aBb0L-0tP_pkqZIbPltb71-u6inawaPfQw', -5, 5);
echo substr($str,0,5).'...'.substr($str,-5)
You can use the following:
if(strlen($input) > 10) {
echo substr($input, 0, 5) . '...' . substr($input, -5);
} else {
echo $input;
}
This will check the length of the $input
first. In case less than 10 characters, that is meaningless to add ...
in the string.
$input = 'APA91bGjUqf8O8yoajdN9BNf2Hs1iVm3VL37X7rn1_XiU1bcKOWVyaIYYusL8f5BCgzSw1HhaPbgntuYHFCR0VWrqGb59nDHMVfgJ-zK0SA0SWw0dvdMEB8AwI-Ltn56aBb0L-0tP_pkqZIbPltb71-u6inawaPfQw';
$output = substr($input, 0, 5) ."...". substr($input, -5);
This is what you need :)
Wayne