2014-02-20 31 views
0

下面是我的工作代碼,它從遠程位置拉取文本文件,並將特定行插入到頁面的html正文中。代碼現在工作得很好。然而,我想對代碼做一個補充,並讓它隨機獲取它所在的行。這是我想要做的。PHP/Curl在兩個標籤之間隨機化文本

被拉動的文本文件將有不同數量的行。只有一條線通過echo $lines[0];選擇,它告訴哪條線。該行將被格式化爲這樣..

<p>This is a line of text <a href="http://domain1.com">domain 1</a>. This is a line of text.</p><p>This is a line of text <a href="http://domain2.com">domain 2</a>. This is a line of text.</p><p>This is a line of text <a href="http://domain3.com">domain 3</a>. This is a line of text.</p> 

所有這一切將是一行,並拉入到頁面的HTML。上面的例子將顯示3段文字,並按照上面的順序鏈接。

我想要做的就是讓文本行在<p>..</p>之間隨機化所以,例如,如果我將下面的代碼放在站點A上,輸出將按照域1,域2和域3的順序排列。如果我把Site BI上的代碼稱爲域3,然後是域1,然後是域2.以隨機順序顯示它們,而不是每次將代碼放在網站上的確切順序。

我不知道是否需要某種緩存在網站上我有代碼要記住隨機顯示哪個順序。這就是我想要的。我不想在每個頁面加載一個隨機順序。

我希望這是有道理的。如果不是,請告訴我,這樣我可以嘗試並更好地解釋它。這是我現在的工作代碼。任何人都可以幫助我做到這一點?非常感謝您的幫助。

<?php 
function url_get_contents ($url) { 
    if (function_exists('curl_exec')){ 
     $conn = curl_init($url); 
     curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true); 
     curl_setopt($conn, CURLOPT_FRESH_CONNECT, true); 
     curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1); 
     $url_get_contents_data = (curl_exec($conn)); 
     curl_close($conn); 
    }elseif(function_exists('file_get_contents')){ 
     $url_get_contents_data = file_get_contents($url); 
    }elseif(function_exists('fopen') && function_exists('stream_get_contents')){ 
     $handle = fopen ($url, "r"); 
     $url_get_contents_data = stream_get_contents($handle); 
    }else{ 
     $url_get_contents_data = false; 
    } 
return $url_get_contents_data; 
} 
?> 

<?php 
$data = url_get_contents("http://mydomain.com/mytextfile.txt"); 
if($data){ 
$lines = explode("\n", $data); 
echo $lines[0]; 
} 
?> 
+0

雖然你說你想輸出隨機化,你實際上去描述的是輸出旋轉。您需要將當前位置存儲在輪換過程中,並在每次輸出時對其進行相應處理。 – JBES

+0

是的,這是更正確的,我問的是如何讓輸出旋轉。這是我需要幫助,因爲我不明白如何完成它。 – Matt

回答

1

嘗試這個

$str = '<p>This is a line of text <a href="http://domain1.com">domain 1</a>. This is a line of text.</p><p>This is a line of text <a href="http://domain2.com">domain 2</a>. This is a line of text.</p><p>This is a line of text <a href="http://domain3.com">domain 3</a>. This is a line of text.</p>'; 

    preg_match_all('%(<p[^>]*>.*?</p>)%i', $str, $match); 

    $count = 0; 
    $used = array(); 
    while ($count < 3) { 
     $index = rand(0, 2); 
     if (!isset($used[$index])) { 
      $used[$index] = 1; 
      echo $match[0][$index]; 
      $count++; 
     } 
    } 
+0

在你的情況'$行[0]'將是'$ str' – Minhaz

1

我想我明白你的要求,但如果沒有,請讓我知道,我會調整。

基本上,我在這裏做的是計算陣列中爆炸的行數,然後用它作爲隨機化的最大數量。一旦我有一個隨機數,那麼我只需訪問該文件數組的那一行。所以如果我生成數字5,那麼它將抓取數組中的第5行。

$lines = explode("\n", $data); 
$line_count = count($lines) - 1; 

for ($i = 0; $i < 3; $i++) { 
    print "<p>".$lines[get_random_line($line_count)]."</p>"; 
} 


function get_random_line($line_count) { 
    mt_srand(microtime() * 1000000); 
    $random_number = rand(0, $line_count); 
    return $random_number; 
} 
1

無需修改代碼太多,沒有進入存儲在數據庫中的值,使用平面文件存儲你可以做類似如下:

創建一個名爲「count.txt」文件,並將其與您的php文件位於同一位置。

<?php 
function url_get_contents ($url) { 
    if (function_exists('curl_exec')){ 
     $conn = curl_init($url); 
     curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true); 
     curl_setopt($conn, CURLOPT_FRESH_CONNECT, true); 
     curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1); 
     $url_get_contents_data = (curl_exec($conn)); 
     curl_close($conn); 
    }elseif(function_exists('file_get_contents')){ 
     $url_get_contents_data = file_get_contents($url); 
    }elseif(function_exists('fopen') && function_exists('stream_get_contents')){ 
     $handle = fopen ($url, "r"); 
     $url_get_contents_data = stream_get_contents($handle); 
    }else{ 
     $url_get_contents_data = false; 
    } 
return $url_get_contents_data; 
} 

$data = url_get_contents("http://mydomain.com/mytextfile.txt"); 

$fp=fopen('count.txt','r');//Open count.txt for reading 
$count=fread($fp,4) ? $count++ : $count=0;//Get and increment $count (4=no. bytes to read) 
fclose($fp); //Close file 

if($data){ 
$lines=explode("\n",$data); 
if($count>count($lines)){$count=0;}//Reset $count if more than available lines 
echo $lines[$count]; 

$fp=fopen('count.txt','w'); //Another fopen to truncate the file simply 
fwrite($fp,$count); //Store $count just displayed 
fclose($fp); //Close file 
} 
?> 
+0

我完全按照上面的方式創建了一個count.txt文件,其中.php文件位於該文件夾中,並用上面的代碼替換了我的代碼。它顯示正在拉入的內容,但它們的順序相同,但沒有更改。 – Matt

+0

是的,以上只是增加了您想要顯示的一個位置的$計數,其中將顯示每個$行的前向順序。如果你想在不同的網站上以相反的順序顯示$行,你可以改變$ count ++爲'$ count - '例如(和'if($ count JBES

0

聽起來像你真的想要一種方式來擁有獨特的內容,或者也可以有更新HTML頁面上的內容的外觀。這對我來說非常有用,我相信很多其他人也會喜歡它,儘管它與你正在嘗試做的有些不同。

這將從文本文件中抓取嵌套的Spintax。它會旋轉內容並顯示在您的頁面中。你的頁面需要是.php,但是有一種方法可以在HTML頁面上工作,這就是我使用它的原因。

Spintax示例:{cat | Dog | Mouse}適用於句子旋轉,旋轉/旋轉圖像,旋轉HTML代碼等......您可以用這些做很多事情。

<?php 
function spin($s){ 
preg_match('#\{(.+?)\}#is',$s,$m); 
if(empty($m)) return $s; 

$t = $m[1]; 

if(strpos($t,'{')!==false){ 
    $t = substr($t, strrpos($t,'{') + 1); 
} 

$parts = explode("|", $t); 
$s = preg_replace("+\{".preg_quote($t)."\}+is",      
$parts[array_rand($parts)], $s, 1); 

return spin($s); 
    } 


$file = "http://www.yourwebsite/Data.txt"; 
$f = fopen($file, "r"); 
while ($line = fgets($f, 1000)) { 
echo spin($line); 
} 





?> 
相關問題