2011-09-19 20 views
-3

我希望製作一個能夠理解分頁的小腳本,以便我永久地輸入並提前分頁。如何製作一個能夠理解分頁的腳本

我輸入例如,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? 
    } 
} 
+1

'[0-9] {1,}' - >'\ d +' – alex

+2

是什麼問題? – arunkumar

+0

上面腳本的輸出最後將是:'hello1.com/14/oijosij'' hello1.com/15/oijosij'' hello1.com/16/oijosij'' hello1.com/17/oijosij'和等等。是不是很清楚,即時對不起 –

回答

0

我敢肯定,必須有一個非常不同的&更簡單的方法來這一點,但我做了我所能做什麼,我知道

<?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>"; 
     } 
    } 
?> 
+0

謝謝你的嘗試。但是當我測試它時它不輸出任何東西。 $部分沒有設置,所以你檢查null的功能? –

+0

很奇怪,正在爲我工​​作:(無論如何,在那裏你看到我分配,檢查將是== 仍然我看到你的編輯有多個字符串的數字,所以我試圖解決 – derp

+0

非常感謝,可能會發生,如果theres日期或東西在網址中,這可能是典型的博客。 –

相關問題