Wednesday, March 13, 2013

Palindromes search

The task is to find all the palindromes (spells the same from right and left) in the given text and print them with the number of occurrences. Minimum length of string is 2 since a character is a palindrome.

Here is an updated solution (thx Denis & Tim for pointing it out.):

// Given a string, test it for Palindromes, then output them along with the number of times they occur?
//sample string
$s = 'cd111 ana 876 qqqqqwq 00112 čćčćććččć把ŠŠđđđđđĐĐĐĐĐđččć';

//array to store results
$res = array();


//borrowed from
// using mb_check_encoding instead of mb_substr ;)
function getCharArray ($jstring) {
  if (mb_strlen($jstring, 'UTF-8') == 0)
    return array();

  $ret = array();
  $alen = strlen($jstring);
  $char = '';
  for ($i = 0; $i < $alen; $i++) {
    $char .= $jstring[$i];
    if (mb_check_encoding($char, 'UTF-8')) {
      array_push($ret, $char);
      $char = '';
  return $ret;

//mimumum lenght is 2
for ($l = 2; $l < mb_strlen($s) + 1; $l++) {
  //get all the posible subsrings with lenght above 2
  for ($i = 0; $i < (mb_strlen($s) + 1 - $l); $i++) {
    $g = mb_substr($s, $i, $l);

    //reverse $g string,
    //could use strrev but it is not multibyte safe
    //$rg = strrev($g);
    //multibyte safe version
    $t = getCharArray($g);
    $t = array_reverse($t);
    $rg = implode($t);

    //compare and store to result array
    if ($g == $rg) {
      //if match
      if (array_key_exists($g, $res)) {
        $res[$g] = $res[$g] + 1;
      else {
        $res[$g] = 1;

//output the results
foreach ($res as $r => $v) {
  echo "$r:$v\n";

Reverse order of words

Here is a simple programming problem. Take the string "Done is better than perfect" and reverse the order of the words. Final output should be "Perfect than better is done"

Here is a very simple solution written in PHP.


//original string
$s= 'Done is better than perfect';
//put all the words to lowercase
$s= mb_strtolower($s); 

//split the string by spaces and put in in an array
$arr = explode(" ", $s);
//reverse sort the array
$arr = array_reverse($arr);

//Put the first char of first word to uppercase

//print the output
echo implode(' ', $arr);

Tuesday, March 5, 2013

PHP getopt alternative

Are you using getopt. It works but if you are stuck in legacy version of PHP you will miss long options. And what if you do not wan't to use '-' as separator. We could use the idea from Bash and array_shift parameters. See the sample bellow.

function DisplayHelp () {
  echo "Usage:\n";
  echo "     -f, --folder folder to produce output to  \n";
  echo "     -s, --server ftp server to use \n";
  echo "     -u, --user ftp server user  \n";
  echo "     -p, --pass ftp server password  \n";
  echo "     --port ftp server port, defaults to 21  \n";
  echo "     -d, --debug debug mode \n";
  echo "     -l, --log location";
  echo "     -h, --help display this help and exit";
  echo "\n";

$options = array();

if (!(php_sapi_name() == 'cli')) {
  echo 'You need to run this from console.';

//remove the name of the executing script

//no options chosen
if (!is_array($argv))

//parse parameters
while (($param = array_shift($argv)) !== NULL) {
  switch ($param) {
    case '--folder':
    case '-f':
      $options['folder'] = array_shift($argv);
    case '--server':
    case '-s':
      $options['server'] = array_shift($argv);
    case '--user':
    case '-u':
      $options['user'] = array_shift($argv);
    case '--pass':
    case '-p':
      $options['pass'] = array_shift($argv);
    case '--port':
      $options['port'] = array_shift($argv);
    case '--debug':
    case '-d':
      $options['debug'] = TRUE;
    case '--log':
    case '-l':
      $options['log'] = array_shift($argv);
    case '--help':
    case '-h':

//show options