Created
June 16, 2013 10:17
-
-
Save eugeneglova/5791615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// How to replace middle part of the word | |
// Array of words | |
$array = array('what', 'aaa', 'hellokity'); | |
// Function to return replaced value | |
// Accept value as a string and | |
// replacer as a character to replace each char in middle part of the word | |
// for example hellokity => hel000ity | |
function convert_string($value, $replacer) { | |
$len = strlen($value); | |
$third = $len / 3; | |
$middle_len = ceil($third); | |
$prefix_len = floor($third); | |
$prefix = substr($value, 0, $prefix_len); | |
$suffix = substr($value, -$prefix_len); | |
return $prefix . str_repeat($replacer, $middle_len) . $suffix; | |
} | |
// Convert each word and output resuts | |
foreach ($array as $value) { | |
echo convert_string($value, '0') . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment