2011-12-14 46 views
0

$消息似乎没有显示任何内容。随机选择的推荐书

我有以下代码:

<?php 
$files = glob("testimonials/*.txt"); 
$filename = $files[rand(0, count($files)-1)]; 
$lines = file($filename); 
$author = array_shift($lines); 
$message = explode("", $lines); 
?> 

<h1>Testimonial</h1> 
<p><b>Author:</b> <?php echo $author; ?></p> 
<?php echo $message; ?> 

在我的推荐文件夹,我的文本文件与作者名字一个第一线,然后直接在该行的消息。

我在这里错过了什么让这个工作正常?

+0

您的移动/删除第一行,你说这就是作者的名字,是在第二行`$ message`的不换行的其余部分?你可以使用`$线[0]`作者姓名,然后`implode($ lines)`到字符串中使用str_replace()从作者信息中删除作者 – 2011-12-14 21:00:09

+0

作者姓名在第一行,其余的信息在第二行;没有换行符,没有额外的空间。让我试试你的答案。 – gstricklind 2011-12-14 21:13:18

回答

0

试试这个:

<?php 
$files = glob("testimonials/*.txt"); 
       //mt_rand is more random 
$filename = $files[mt_rand(0, count($files)-1)]; 

$lines = file($filename); 
//Get authors name 
$author=$lines[0]; 
/*Glue the whole $lines array into a string & remove the author, 
    so whats left is just the message*/ 
$message=str_replace($author,'',implode(' ',$lines)); 
?> 

<h1>Testimonial</h1> 
<p><b>Author:</b> <?php echo $author; ?></p> 
<?php echo $message; ?> 
0

这是我所用,但我没有太多的推荐呢!

function getTestimonial(){ 
// testimonials for Stay-in-Touch.ca  
$q = array(); 
$q[] = " I was very impressed by your gadget - Grandma used it to show me 
      loads of new photos of her latest batch of great-grandchildren - it's 
      just the thing for her.<br>Philip Exeter, UK "; 
$q[] = "The best time for the tablet was when my mom was in the hospital for three weeks before 
    and after her procedure. J went all around my mom&#39;s house, taking pictures of all her plants with 
    her iPhone (many many plants, and my mom loves them all) and uploading them to the tablet. My mom 
    would just lay in her bed and see how her plants were doing (comments like &#39;oh, it&#39;s flowering 
    this year!&#39;), and it would calm her down. <br> M Toronto"; 
$q[] = "Mum really likes the pictures of the new babiy in the family. She can see how fast he is growing.<br> J Montreal"; 
$thisOne = rand(0,count($q)-1); 
return $q[$thisOne]; 

}