[Solved] How to Sort array string by different point in the string


Searching last "some text in quotas" in strings and compare:

function compare($a,$b) 
{
    // searching commentaire (searching last " and second from end ")

    $end_commentaire = strrpos($a, '"');
    $substr_a = substr($a, 0, $end_commentaire);
    $begin_commentaire = strrpos($substr_a, '"');   
    $substr_a = substr($substr_a, $begin_commentaire+1);

    $end_commentaire = strrpos($b, '"');
    $substr_b = substr($b, 0, $end_commentaire);
    $begin_commentaire = strrpos($substr_b, '"');   
    $substr_b = substr($substr_b, $begin_commentaire+1);

    //echo "|$substr_a|$substr_b|<br>";

    // compare 

    $result = strcmp($substr_a, $substr_b);

    // $substr_a and $substr_b are different,
    // don't compare rest of string 

    if( $result != 0 )
         return $result;

    // $substr_a and $substr_b are the same,
    // compare rest of string 

    return strcmp($a, $b);
}

usort($out, 'compare');

Put this in place of sort($out)

Edit:

Using RegEx:

function compare($a,$b) 
{
    // searching commentaire (searching last " and second from end ")

    preg_match("https://stackoverflow.com/"([^"]*)"[^"]*$/', $a, $result_a);
    preg_match("https://stackoverflow.com/"([^"]*)"[^"]*$/', $b, $result_b);

    //echo "|$result_a[1]|$result_b[1]|$result_a[0]|$result_b[0]|<br>";

    // compare 

    $result =  strcmp($result_a[1], $result_b[1]);

    // $result_a[1] and $result_b[1] are different,
    // don't compare rest of string 

    if( $result != 0 )
         return $result;

    // $result_a[1] and $result_b[1] are the same,
    // compare rest of string 

    return strcmp($a, $b);
}

usort($out, 'compare');

12

solved How to Sort array string by different point in the string