使用PHP将部分文本回显到另一个页面或Div

时间:2012-10-25 20:25:36

标签: php echo

我是PHP的新手,任何帮助或方向都会很棒!我不需要答案,只需要一些输入。我想将一页文本的一部分显示在另一页的div上。

第1页

<div id="content">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium
laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis 
architecto beatae vitae dicta sunt laudantium, totam rem aperiam, eaque ipsa quae ab     illo 
</div>

第2页

<div id="portionofcontent">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium...
</div> 

2 个答案:

答案 0 :(得分:2)

将您想要拉入各个页面的文本放入名为whatever.php的文件中,然后在div中使用<?php include('whatever.php'); ?>

更新: 您可以使用以下代码创建名为yourtext.php的文件:

<?php $text = 'Put the text you want to display on multiple pages here.'; ?>

然后将<?php include('yourtext.php'); ?>放在文件的标题中,并在要显示文字的页面内容中使用<?php echo $text; ?>

答案 1 :(得分:0)

如果它是静态文本,那么它几乎就是ennui所说的,但我会采取另一种方式:

还有很多方法可以读取文件。如果它只是纯文本,那么file_get_contents($file)假设它不是一个大文件可能会更好。

试试这个:

content.txt

This is part of a rather long sentence which goes on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on and on until finally it reaches the end

page1.php中

<?php  
 $txt = file_get_contents('./content.txt');
 echo $txt;
?>

使page2.php

<?php    
 $txt = file_get_contents('./content.txt');
 echo substr($txt, 0, 100); //substring(text, start, length) nb. start is from 0
?>

您还可以使用fopenfgets等命令打开文件并只读取文件的一部分,但我认为在此阶段您可能会有点复杂