Jump to content

how to get 3 letters php function


ctcp

Recommended Posts

Not sure if this is what you want. try this

<?php 
$len = strlen($url);
$hidden_url = ""; 
for($cnt = 0; $cnt < 11; $cnt++)
{
    $hidden_url .= '*';
}
$hidden_url .= substr($url, 11, 13);
for($cnt =14; $cnt < $len; $cnt++)
{
    $hidden_url .= '*';
}
?>

I would use str_repeat for this:

 

<?php
function hide($string, $start = 11, $length = 3, $repeater = "*"){
$front = str_repeat($repeater, $start);
$letters = substr($string, $start, $length);
$end = str_repeat($repeater, strlen($string)-($start+$length));
return $front.$letters.$end;
}

$strings = array("http://www.google.com", "http://www.yahoo.com", "http://phpsnips.com/forums");
foreach($strings as $string){
echo hide($string)."<br>";
}
?>

The above function has a bug that will generate an error if the input is less than the length. This should work (tested):

function maskString($inputStr, $showLen, $replaceChar='*')
{
    $inputLen = strlen($inputStr);
    if($inputLen < $showLen) { return $inputStr; }
    $strIdx = ceil(($inputLen-$showLen)/2);
    $showStr = substr($inputStr, $strIdx, $showLen);
    $outputStr = str_repeat($replaceChar, $inputLen);
    return substr_replace($outputStr, $showStr, $strIdx, $showLen);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.