John Davidson

php - How do I hide name surname information with substr?

0 comments
Message:


My code allows me to hide the name Surname information with stars. But its output is as nameSurname >> n ****** e. I want it to be n **** S **** e. So I want the first letter of the last name not to be hidden. How can I do that?


$len = strlen($data->userFullName);
$yildizlanmis = substr($data->userFullName, 0, 1).str_repeat('*', $len - 2).substr($data->userFullName, $len - 1, 1); //NameSurname >> N*******e


My Answer:
To achieve the desired output where only the characters between the first and last letters of the last name are hidden with stars, you can modify the substr function parameters as follows:

php
$len = strlen($data->userFullName);
$spacePos = strpos($data->userFullName, ' '); // Find the position of the space between first and last name
$yildizlanmis = substr($data->userFullName, 0, 1) . str_repeat('*', $spacePos - 1) . substr($data->userFullName, $spacePos, $len - $spacePos - 1) . substr($data->userFullName, $len - 1, 1);


This code snippet finds the position of the space between the first and last name using the `strpos` function and then hides the characters between the first and last letters of the last name with stars.

Rate this post

3 of 5 based on 5947 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us