Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace letters with asterisks except first and last character (mysqli_query)

How can I Replace letters with asterisks except first and last character
for example: house -> h***e Database

<div id="ld">    
        <?php
    $base=mysqli_connect('localhost','root','','exercise');
    $request="select text from exercise";
       /*
       Here I want to show all words from database but with letters replaced
       with asterisks (expect first and last letter)
      */
    mysqli_close($base)
        ?>
  </div>

EDITED CODE:

<div id="ld">    
        <?php
    $base=mysqli_connect('localhost','root','','exercise');
    $request="select text from exercise";
    $str=mysqli_query($base, $request);
    function get_starred($str) {
        $len = strlen($str);
    
        return substr($str, 0, 1).str_repeat('*', $len - 2).substr($str, $len - 1, 1);
    }
    
    
    $myStr = $str;
    echo get_starred($myStr);
    mysqli_close($base)
        ?>
 
  </div>

and error: Fatal error: Uncaught TypeError: strlen(): Argument #1 ($str) must be of type string, mysqli_result given on Line 7

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a way to do it in mysql. Other answers may give PHP solution, but in mysql:

SELECT CONCAT(LEFT(text,1), REPEAT('*',LENGTH(text)-2), RIGHT(text,1)) AS text FROM exercise

simply concatenate the first letter, then your asterisks, then last letter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading