Skip to content

Instantly share code, notes, and snippets.

@Xiradorn
Last active September 12, 2019 16:44
Show Gist options
  • Save Xiradorn/a158eabb503ba9491739f85c83044d76 to your computer and use it in GitHub Desktop.
Save Xiradorn/a158eabb503ba9491739f85c83044d76 to your computer and use it in GitHub Desktop.
simple array algorithm
<?php
/**
* Simple inversion algo
*/
/**
* Function for string inversion.
* @param string $str just a normal text string
* @return string $str same string but inverted
*/
function xir_ary_inv( string $str )
{
$str = str_split( $str );
$max = count( $str );
$middle = ( $max % 2 ) ? ( $max - 1 ) / 2 : $max / 2;
for ( $i = 0; $i < $middle; $i++ )
{
if ( ( $max % 2 ) == 0 and $i == ( $max / 2 ) ) continue;
$n = $max - 1;
$t = $str[ $i ];
$str[ $i ] = $str[ $n - $i ];
$str[ $n - $i ] = $t;
}
$str = implode( '', $str );
return $str;
}
echo xir_ary_inv( "Tony" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment