Skip to content

Instantly share code, notes, and snippets.

@eugeneglova
Created June 16, 2013 10:17
Show Gist options
  • Save eugeneglova/5791615 to your computer and use it in GitHub Desktop.
Save eugeneglova/5791615 to your computer and use it in GitHub Desktop.
<?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