PHP/Curl可在两个标记之间随机化文本
下面是我的工作代码,它从远程位置提取文本文件,并在页面的html正文中插入特定行。代码现在运行得很好。我想做一个额外的代码,但是,让它随机化的行,它得到。这就是我想要做的 正在提取的文本文件将具有不同数量的行。通过PHP/Curl可在两个标记之间随机化文本,php,random,Php,Random,下面是我的工作代码,它从远程位置提取文本文件,并在页面的html正文中插入特定行。代码现在运行得很好。我想做一个额外的代码,但是,让它随机化的行,它得到。这就是我想要做的 正在提取的文本文件将具有不同数量的行。通过echo$行[0]只选择一行它告诉您要获取哪一行。行的格式如下 <p>This is a line of text <a href="http://domain1.com">domain 1</a>. This is a line of text.&
echo$行[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段带有链接的文本
我想做的是让那行文本在之间随机化。
因此,例如,如果我将下面的代码放在站点A上,输出将按域1、域2和域3的顺序排列。如果我把代码放在站点B上,我希望它是域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];
}
?>
试试这个
$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++;
}
}
$str=”这是一行文本。这是一行文字。这是一行文字。这是一行文字。这是一行文字。这是一行文字。';
preg_match_all('%(]*>.*?)%i',$str,$match);
$count=0;
$used=array();
而($count<3){
$index=rand(0,2);
如果(!isset($used[$index])){
$used[$index]=1;
echo$match[0][$index];
$count++;
}
}
试试这个
$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++;
}
}
$str=”这是一行文本。这是一行文字。这是一行文字。这是一行文字。这是一行文字。这是一行文字。';
preg_match_all('%(]*>.*?)%i',$str,$match);
$count=0;
$used=array();
而($count<3){
$index=rand(0,2);
如果(!isset($used[$index])){
$used[$index]=1;
echo$match[0][$index];
$count++;
}
}
试试这个
$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++;
}
}
$str=”这是一行文本。这是一行文字。这是一行文字。这是一行文字。这是一行文字。这是一行文字。';
preg_match_all('%(]*>.*?)%i',$str,$match);
$count=0;
$used=array();
而($count<3){
$index=rand(0,2);
如果(!isset($used[$index])){
$used[$index]=1;
echo$match[0][$index];
$count++;
}
}
试试这个
$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++;
}
}
$str=”这是一行文本。这是一行文字。这是一行文字。这是一行文字。这是一行文字。这是一行文字。';
preg_match_all('%(]*>.*?)%i',$str,$match);
$count=0;
$used=array();
而($count<3){
$index=rand(0,2);
如果(!isset($used[$index])){
$used[$index]=1;
echo$match[0][$index];
$count++;
}
}
我想我理解你的要求,但如果不理解,请告诉我,我会调整
基本上,我在这里做的是计算你分解的数组中的行数,然后用它作为一个最大数来随机化。一旦我有了一个随机数,我就可以访问文件数组的那一行。因此,如果我生成数字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;
}
$lines=explode(“\n”,$data);
$line\u count=计数($line)-1;
对于($i=0;$i<3;$i++){
打印“”$line[获取随机行($line\u计数)]。“”;
}
函数get_random_line($line_count){
mt_srand(微时间()*1000000);
$random\u number=rand(0,$line\u计数);
返回$random_编号;
}
我想我理解你的要求,但如果不理解,请告诉我,我会调整
基本上,我在这里做的是计算你分解的数组中的行数,然后用它作为一个最大数来随机化。一旦我有了一个随机数,我就可以访问文件数组的那一行。因此,如果我生成数字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;
}
$lines=explode(“\n”,$data);
$line\u count=计数($line)-1;
对于($i=0;$i<3;$i++){
打印“”$line[获取随机行($line\u计数)]。“”;
}
函数get_random_line($line_count){
mt_srand(微时间()*1000000);
$random\u number=rand(0,$line\u计数);
返回$random_编号;
}
我想我理解你的要求,但如果不理解,请告诉我,我会调整
基本上,我在这里做的是计算你分解的数组中的行数,然后用它作为一个最大数来随机化。一旦我有了一个随机数,我就可以访问文件数组的那一行。因此,如果我生成数字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;
}
$lines=explode(“\n”,$data);
$line\u count=计数($line)-1;
对于($i=0;$i<3;$i++){
打印“”$line[获取随机行($line\u计数)]。“”;
}
函数get_random_line($line_count){
mt_srand(微时间()*1000000);
$random\u number=rand(0,$line\u计数);
返回$random_编号;
}
我想我理解你的要求,但如果不理解,请告诉我,我会调整
巴斯