如何制作一个能够理解分页的脚本

发布于 2024-12-05 18:25:57 字数 977 浏览 0 评论 0原文

我希望制作一个小脚本来理解我输入的网址的分页并永远推进分页。

我的输入示例,2 个字符串:

hello14.com/14/oijosij
hello14.com/15/oijosij

脚本应输出:

hello14.com/14/oijosij
hello14.com/15/oijosij
hello14.com/16/oijosij
hello14.com/17/oijosij
hello14.com/18/oijosij
hello14.com/19/oijosij

和永远永远。

它必须能够处理 url 的多种变体,而不仅仅是这种特定情况。

如果您查看下面的代码,您就会看到我遇到麻烦的地方。 它检测网址中是否有分页,但我不知道如何检测分页所在的正确位置。 我不能直接分解找到的分页号,这会导致错误,因为我的示例也包含网址中其他位置的第一个分页号。

$string1 = "hello14.com/14/oijosij";
$string2 = "hello14.com/15/oijosij";

// match all spans of numbers
preg_match_all("/[0-9]{1,}/", $string1, $out);
preg_match_all("/[0-9]{1,}/", $string2, $out2);

// loop through all spans of numbers found in string 1
for ($loop = 0; $loop < count($out[0]); $loop++) {
  if ($out2[0][$loop] - $out[0][$loop] == 1) {
    echo "we have pagination. ".$out[0][$loop]." and ".$out2[0][$loop];
    // but how can i go about it here?
  }
}

i looking to make a small script that will understand pagination of urls i input and advance the pagination forever.

my input example, 2 strings:

hello14.com/14/oijosij
hello14.com/15/oijosij

script should output:

hello14.com/14/oijosij
hello14.com/15/oijosij
hello14.com/16/oijosij
hello14.com/17/oijosij
hello14.com/18/oijosij
hello14.com/19/oijosij

and forever and forever.

it must be able to handle many variations of urls, not just this spesific case.

if you look at my code below, you see where i run into trouble.
it detects if there IS pagination in the urls, but i dont know how to detect the right position of where the pagination is located.
i cannot just explode the pagination number found, that will result in error, as my example contains the first pagination-number also elsewhere in the url.

$string1 = "hello14.com/14/oijosij";
$string2 = "hello14.com/15/oijosij";

// match all spans of numbers
preg_match_all("/[0-9]{1,}/", $string1, $out);
preg_match_all("/[0-9]{1,}/", $string2, $out2);

// loop through all spans of numbers found in string 1
for ($loop = 0; $loop < count($out[0]); $loop++) {
  if ($out2[0][$loop] - $out[0][$loop] == 1) {
    echo "we have pagination. ".$out[0][$loop]." and ".$out2[0][$loop];
    // but how can i go about it here?
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-12-12 18:25:57

我确信一定有一种非常不同且更简单的方法来解决这个问题,但我用我所知道的做了我能做的

 <?php
    //into an ugly function...
    function get_structure($str1, $str2){
        preg_match_all("/\d+/",$str1, $out1);
        preg_match_all("/\d+/",$str2, $out2);
        $parts = preg_split("/\d+/",$str1);
        $total = count($out1[0]);
        for($i=0;$i<$total;$i++){
            if($out2[0][$i] - $out1[0][$i] == 1){
                $left = "";
                for($j=0;$j<=$i;$j++){
                    $left .= $j == $i ? $parts[$j] : $parts[$j].$out1[0][$j];
                }
                $right = "";
                for($j=$i+1;$j<=$total;$j++){
                    $right .= $j == $total ? $parts[$j] : $parts[$j].$out1[0][$j];
                }
                return array($left, $right, $out2[0][$i]);
            }   
        }
        return false;
    }


    //using it
    $str1 = "13hello1415duh.com/2011/13/yay";
    $str2 = "13hello1415duh.com/2011/14/yay";
    if($parts = get_structure($str1, $str2)){
        list($left,$right,$number) = $parts;
        while($number < 30){ //it has to stop..
            $number++;
            echo $left.$number.$right."<br>";
        }
    }
?>

I'm sure there must be a very different&easier approach to this, but I did what I could with what I knew

 <?php
    //into an ugly function...
    function get_structure($str1, $str2){
        preg_match_all("/\d+/",$str1, $out1);
        preg_match_all("/\d+/",$str2, $out2);
        $parts = preg_split("/\d+/",$str1);
        $total = count($out1[0]);
        for($i=0;$i<$total;$i++){
            if($out2[0][$i] - $out1[0][$i] == 1){
                $left = "";
                for($j=0;$j<=$i;$j++){
                    $left .= $j == $i ? $parts[$j] : $parts[$j].$out1[0][$j];
                }
                $right = "";
                for($j=$i+1;$j<=$total;$j++){
                    $right .= $j == $total ? $parts[$j] : $parts[$j].$out1[0][$j];
                }
                return array($left, $right, $out2[0][$i]);
            }   
        }
        return false;
    }


    //using it
    $str1 = "13hello1415duh.com/2011/13/yay";
    $str2 = "13hello1415duh.com/2011/14/yay";
    if($parts = get_structure($str1, $str2)){
        list($left,$right,$number) = $parts;
        while($number < 30){ //it has to stop..
            $number++;
            echo $left.$number.$right."<br>";
        }
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文