PHP: Dynamically Transforming a String

18 06 2017

Flickr: by Gnu200

Some people may be less than enamored of academic computer programming questions, preferring instead those that are pertinent to the issues they deal with in their working lives. I’ve found however, that sometimes an academic question can end up enhancing one’s comprehension. In this vein, consider a situation where a barely decipherable string needs to be rendered into a format that is more legible without inserting spaces or creating a copy of the string.

The following code aims to achieve the above-described transformation with this string:

“THERAININSPAINSTAYSMAINLYINTHEPLAIN.”

<?php

function buildPosArr( $s, $d ) {

  $pArr = [];
  $needle = "";
  $endstr = 0;
  for( $i=0, $max=count( $d ); $i < $max-1; $i++) {
    $needle = $d[$i];
    if( ($pos = stripos( $s, $needle ) ) !== false){
      $pArr[$pos] = $needle;
      if ($needle == "plain") break; // done
        for ( $j = $pos, $max = ($pos + strlen($needle)); $j <   $max; $j++) {
          $s[$j] = "\0";
        } // end for
      }// end if
  }// end for
  return $pArr;
}

function assign2str(&$s,$k,$v){
  $max = strlen($v);
  for($i = $k, $j=0; $j < $max; $j++,$i++) {
     $s[$i] = $v[$j];
   }
}

// Start here:<img src="https://c2.staticflickr.com/6/5275/5882079207_f07920c392_n.jpg" title="Flickr: by Gnu200" />
$str = "THERAININSPAINSTAYSMAINLYINTHEPLAIN.";
echo "Before: $str\n\n";

$d = ["The", "rain", "in",
      "Spain", "stays", "mainly",
      "in", "the", "plain"];

$arr = buildPosArr( $str, $d );

foreach ($arr as $key => $value) {
  switch($value) {
  case "The": $value = strtolower($value); break;
  case "rain": $value = ucfirst($value); break;
  case "in": $value = ucfirst($value); break;
  case "Spain": break;
  case "stays": $value = strtoupper($value); break;
  case "mainly": break;
  case "plain": $value = ucfirst($value);
  default: break;
  }
  assign2str( $str,$key,$value );
}

echo " After: ",$str;

See demo

The array $d contains every word in the string intentionally. It is used subsequently in buildPosArr() in order to locate every word in the string and build another array to hold that information which becomes the function’s return value. buildPosArr() searches for each word using the words in $d as the needles for the haystack of the allcaps string. Since stripos() will seek on a case-insensitive basis the first instance of a needle, as each needle is found, it is zeroed out in the duplicate allcaps string, so as to be able to locate a subsequent needle with the same word value.

The returned array then has its values either lowerecased, uppercased, initial-capped or left unaltered. At the point, one could implode the array and voila mission accomplished efficiently — except this assignment involves altering the original string. For this purpose, assign2str() at the bottom of the for-loop comes in handy. It uses the $key and $value variables to alter the original string. The key holds the position of the word in the larger string while the value is the word itself. Each character of every value is copied into the corresponding word of the original string to produce the following striking result:

Before: THERAININSPAINSTAYSMAINLYINTHEPLAIN.

After: theRainInSpainSTAYSmainlyInthePlain.

This work is licensed under a Creative Commons License

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: