简体   繁体   English

如何使用php和preg_match在URL中按ID排序

[英]How to sort by ID in url using php and preg_match

I have a php working code where I take an array full of URLs, I use preg_match to obtain all the information within the URL for example a typical url looks like this: 我有一个php工作代码,其中包含一个充满URL的数组,我使用preg_match获取URL中的所有信息,例如,典型的URL如下所示:

http://photos.com/path=images&user=Ron197&userid=970965&hue=soft http://photos.com/path=images&user=Ron197&userid=970965&hue=soft

To match the userid I use this within a foreach loop: 为了匹配用户标识,我在foreach循环中使用了它:

preg_match_all('/userid=(.+?)&hue/', $linklong, $userid);
        $userid = $userid[1][0];
        //print_r($userid);

The main array is sorted in alphabetical order with more than 600 urls and it displays just fine like that, but I'm trying to also display the entire list of URLs sorted by userid. 主数组以超过600个URL的字母顺序排序,并且显示得很好,但是我试图同时显示按userid排序的URL的整个列表。

Can anyone help at least get me started I tried using ksort, usort, but I honestly don't get the logic behind it The idea is that linklong would need to be rearranged by userid 任何人都至少可以帮助我入门,我尝试过使用kso​​rt,usort,但是老实说我没有得到背后的逻辑。这个想法是linklong需要由userid重新排列

First, you should use parse_url to extract your 'query' part. 首先,您应该使用parse_url提取“查询”部分。 Then, using parse_str , you can separate all parameters. 然后,使用parse_str ,可以分隔所有参数。

This allow you to check that your URL has is valid. 这使您可以检查您的URL是否有效。

Then, if you want to be able to sort the array on any field you want (and not only on the userId ) you just have to create a callback function that will take 2 elements of your array and return -1, 0 or 1 respectivelly if the first element is <, =, > than the second one. 然后,如果您希望能够在所需的任何字段(不仅是userId )上对数组进行排序,则只需创建一个回调函数,该函数将接收数组的2个元素并分别返回-1、0或1如果第一个元素比第二个元素<,=,>。

Then, to order your array, you just have to call the function usort : 然后,要订购数组,只需调用函数usort

bool usort ( array &$array , callable $cmp_function )

giving your array as first parameter and your callback function as second parameter. 将数组作为第一个参数,并将回调函数作为第二个参数。 This allow you to create any comparision callback you want and order your array like you want. 这使您可以创建所需的任何比较回调,并根据需要对数组进行排序。

Note: Take care of the performance.. If your callback do a lot of stuff, the sort will take some time because the callback function is called a lot of time between elements of your array, following a specific sorting algorithm (like the bubbling sort). 注意:请注意性能。如果您的回调执行大量工作,则排序将花费一些时间,因为遵循特定的排序算法(例如冒泡排序),回调函数在数组元素之间被调用了很多时间)。

An example of a such comparision function: 此类比较函数的示例:

function compareUrlCustom($u1, $2)
{
    // Parse the 2 URLs
    $urlParts1 = parse_url($u1);
    $urlParts2 = parse_url($u2);

    // Extract and parse the 2 query parts
    $queryParts1 = array();
    $queryParts2 = array();
    parse_str($urlParts1['query'], $queryParts1);
    parse_str($urlParts2['query'], $queryParts2);

    // Return 1, O or -1 as comparision value
    if ($queryParts1['userId'] > $queryParts2['userId'])
        return 1;
    else if ($queryParts2['userId'] > $queryParts1['userId'])
        return -1;
    else
        return 0;
}

And then, you can call the sort of your array with: 然后,您可以使用以下命令调用数组的排序:

$wellSorted = usort($yourArray, compareUrlCustom);

and check $wellSorted to know if the sort operation is a success or not. 并检查$wellSorted以了解排序操作是否成功。

Note: you should add some check in the compareUrlCustom function to be sure to have valid URL and eventually throw an exception if its not the case. 注意:您应该在compareUrlCustom函数中添加一些检查,以确保具有有效的URL,如果不是这种情况,最终将引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM