ctcp Posted November 2, 2011 Share Posted November 2, 2011 Hello my mysql db got row pageurl pageurl http://www.google.gr http://www.yahoo.com http://www.phpfreaks.com i whant to echo 11,12,13 letter only ***********goo****** but i whant to show and count the ******* how can i do this? any idea? Link to comment https://forums.phpfreaks.com/topic/250296-how-to-get-3-letters-php-function/ Share on other sites More sharing options...
jotorres1 Posted November 2, 2011 Share Posted November 2, 2011 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 .= '*'; } ?> Link to comment https://forums.phpfreaks.com/topic/250296-how-to-get-3-letters-php-function/#findComment-1284291 Share on other sites More sharing options...
The Little Guy Posted November 2, 2011 Share Posted November 2, 2011 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/250296-how-to-get-3-letters-php-function/#findComment-1284306 Share on other sites More sharing options...
Psycho Posted November 2, 2011 Share Posted November 2, 2011 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); } Link to comment https://forums.phpfreaks.com/topic/250296-how-to-get-3-letters-php-function/#findComment-1284322 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.