Hello! I'm new to the forum, but I'm really excited about being part of the community here.
So here's my question. I'm trying to order string based on a custom alphabet. I have php code that breaks the file to be sorted into an array of strings. I'm attempting to order each string. The strings are ordering themselves correctly, but they are moving themselves around in the array somehow. I'm new to php, so I'm hoping someone could show me my errors.
Thanks
<?php
echo " start ";
$file = fopen('inFile.txt', "r");
$firstLine = fgets($file);
echo " hi ";
$x = 0;
while(! feof($file)){
$restOfFile[$x] = fgets($file);
$x++;
}
$firstLine = str_replace(' ','',$firstLine);
fclose($file);
//echo $firstLine;
//print_r( $restOfFile);
function mylst($a,$b){
$pos1 = 0;
$pos2 = 0;
global $firstLine;
if(strlen($a) > strlen($b)){
$string = $b;
}else{
$string = $a;
}
for ($i=0;$i<(strlen($string) && $pos1===$pos2); $i++){
$pos1 = strpos($firstLine,substr($a,1));
$pos2 = strpos($firstLine,substr($b,1));
}
if ($pos1 === $pos2 && strlen($a) !== strlen($b)){
return (strlen($a)-strlen($b));
}
return $pos1-$pos2;
}
echo " BEFORE ";
print_r($restOfFile);
echo " AFTER ";
usort($restOfFile,"mylst");
print_r($restOfFile);
?>