Replace last 3 characters (at most) with asterisks for multiple elements in each result set row

178 Views Asked by At

I have this PHP code with MySQL database query and I would like to display a record from MySQL database such as first_name and last_name records, I would like only to display like PHP echo $row the first 3 letters of the first name and last name output, something like this:

Joh*** Smi***

Jef*** Joh***
I saw this example online, I tried it and it works! but just need to replace the example value of '$row = "Hamingway"' to fetch the real array value from MySQL Database instead of: "Hamingway" `Ham****** `**

$row = "Hamingway";
echo (strlen($row) > 3) ? substr($row, 0, 3) . str_repeat('*', strlen($row) - 3) : str_repeat('*', strlen($row));

'John Smith' needs to convert to display output ---> 'Joh... Smi...' 'Jeff Johnson' needs to convert to display output ---> 'Jef... Joh...'

I currently presenting rows of data from a MySQL query in an HTML table with the following structure:

<tr>
    <td><?php echo $row['first_name']; ?></td>
    <td><?php echo $row['last_name']; ?></td>
    <td><?php echo $row['last_number']; ?></td>
</tr>

My current code looks like this:

require_once 'includes/db.php';
$query = "SELECT * FROM sign_in3 ORDER BY date ASC LIMIT 5";
$results = mysqli_query($conn, $query); 
$records = mysqli_num_rows($results);
if (!empty($records)) { 
    while ($row = mysqli_fetch_assoc($results)) { ?>
        <tr style="font-size:40px;">
            <td><center><?php echo $row['first_name']; ?></center></td>
            <td><center><?php echo $row['last_name']; ?></center></td>
            <td><center><?php echo $row['last_number']; ?></center></td>
        </tr>
    <?php }
} ?>

I would like to see only the first 3 letters from the first name and last name values.

2

There are 2 best solutions below

5
Theo On

You can try this below assume $result is the result of your MySQL query, fetched as an associative array. In this case you would have to adjust the keys accordingly based on your actual database structure.

<?php

    $result = [
    'first_name' => 'John',
    'last_name' => 'Smith'
    ];

    // Process and display the first 3 letters of the first name and last name
    $firstName = $result['first_name'];
    $lastName = $result['last_name'];

    $processedFirstName = (strlen($firstName) > 3) ? substr($firstName, 0, 3) . str_repeat('*', strlen($firstName) - 3) : str_repeat('*', strlen($firstName));
    $processedLastName = (strlen($lastName) > 3) ? substr($lastName, 0, 3) . str_repeat('*', strlen($lastName) - 3) : str_repeat('*', strlen($lastName));

    echo $processedFirstName . ' ' . $processedLastName;

?>
10
mickmackusa On

I am surprised that you weren't able to swap in your result set variables with the variables in your posted code and you didn't show how you are iterating your result set, so I guess I'll spell it all out from the querying step. I want to also show you how to more concisely implement your masking logic in a single function call.

Your desired implementation of asterisks does not match your coding attempt, so I'll write my own regex pattern to truncate the name strings after their third character and unconditionally append 3 asterisks.

Code: (PHPize Demo with dummy data)

function mask($value)
{
    return preg_replace('/^.{0,3}\K.*/', '***', $value);
}

$result = mysqli_query($conn, "SELECT * FROM sign_in3 ORDER BY date LIMIT 5"); 
foreach ($result as $row) {
    printf(
        "<tr>
             <td>%s</td>
         </tr>
         <tr>
             <td>%s</td>
         </tr>\n",
        mask($row['first_name']),
        mask($row['last_name']))
    );
}

From "John Smith" and "Jeff Johnson", you'll get single-column rows.

<tr><td>Joh***</td></tr>
<tr><td>Smi***</td></tr>
<tr><td>Jef***</td></tr>
<tr><td>Joh***</td></tr>