D
D
doktorpull2013-12-11 19:12:48
PHP
doktorpull, 2013-12-11 19:12:48

How to output a certain number of characters from a text file?

Hello, I'm not very good at php, but there was a need to load data from a text file to the site. In general, here is the problem.
You need to take 300 characters from a text file, keeping all paragraphs and spaces. if there are more characters in the file than can be displayed on the page, then another page is created on which the continuation of the text is displayed. I would be grateful if you could help me with this solution. I found the code, but it doesn't quite suit me because it displays a certain number of lines.

<?php
if (!defined('DATALIFEENGINE')) die("Go fuck yourself!");



require ENGINE_DIR . '/data/config.php';

  $newsid = intval($_REQUEST['id']);

  $row2 = $db->super_query("SELECT id, date, onserver, news_id FROM " . PREFIX . "_files WHERE news_id ='$newsid'");
    
  $book = $row2['onserver'];
  
  $file = ROOT_DIR.'/uploads/files/'.$book;
    if(file_exists($file)) $book_data = str_replace("\n" ,"<br/><br/>\n",file_get_contents($file));
    else $book_data = "указанная книги не найдена";
    
    /*echo $book_data;*/
    //print ("<div  class=eventNotifier>Ctrl+right</div>");
    /*[email protected]($file);*/
    $filelink = str_replace("\f" ,"<br/><br/>\n",@file($file));
    
// считаем кол-во строк
$count=count($filelink);

// максимальное кол-во строк на странице
$max="20";

// вводим переменную, указывающую номер страницы
if(empty($_GET["p"])){$_GET["p"]="1";}
$p=$_GET["p"];

// защита от ввода вместо номера страницы какой-нибудь ерунды
if(preg_match("/[%[email protected],^=:;а-я\"*()&$#№+\-!?<>\~`|[{}\]]/i", $p))
{die(header("Location:index.php?do=book&id=".$newsid.""));}

// если кто-то вводит номер страницы больше, чем всего у нас страниц
$total=ceil($count/$max);
if($p>$total){$p=$total;}

$first=$p*$max-$max;
$last=($p*$max)-1;

 for($i=$first;$i<=$last;$i++)
{
if(@$filelink[$i]){@$list= "$filelink[$i]";}
 
}
 for($i=$first;$i<=$last;$i++) { 
$text = $text.$filelink["$i"]; 
}



//$text = fgets($filelink, 300);
/*
#две назад
print "<div>";
  if(($p-2)>0):
  $ptwoleft="<a class='first_page_link' href='index.php?do=book&id=".$newsid."&p=".($p-2)."'>".($p-2)."</a> ";
else:
  $ptwoleft=null;
endif;
  */	
#одна назад
if(($p-1)>0):
  $poneleft="<a class='prev-link' href='index.php?do=book&id=".$newsid."&p=".($p-1)."'>".($p-1)."</a>  ";
  $ptemp=($p-1);
else:
  $poneleft=null;
  $ptemp=null;
endif;
    
#две вперед
/*if(($p+2)<=$total):
  $ptworight="  <a class='first_page_link' href='index.php?do=book&id=".$newsid."&p=".($p+2)."'>".($p+2)."</a>";
else:
  $ptworight=null;
endif;
  */	
#одна вперед
if(($p+1)<=$total):
  $poneright="<a class='next-link' href='index.php?do=book&id=".$newsid."&p=".($p+1)."'>".($p+1)."</a>";
  $ptemp2=($p+1);
else:
  $poneright=null;
  $ptemp2=null;
endif;		
    
/*# в начало
if($p!=1 && $ptemp!=1 && $ptemp!=2):
  $prevp="<span id='first_page_link'><a href='index.php?do=book&id=".$newsid."&p=1' class='first_page_link' title='В начало'><<</a></span> ";
else:
  $prevp=null;
endif;   
    
#в конец 
if($p!=$total && $ptemp2!=($total-1) && $ptemp2!=$total):
  $nextp=" ...  <span id='first_page_link'><a href='index.php?do=book&id=".$newsid."&p=".$total."' class='first_page_link'>$total</a></span>";
else:
  $nextp=null;
endif;
  */	
$endpegs = "<br>".$prevp.$ptwoleft.$poneleft.'<span class="num_page_not_link"><b>'.$p.'</b></span>'.$poneright.$ptworight.$nextp; 
print "</div>";
$tpl->load_template('book.tpl');
//$tpl->compile('content');
 
//$tpl->set('{test}',      "Чтото");
//$tpl->set('{test2}', "Второе чтото");
$tpl->set('{endpegs}',$endpegs);
$tpl->set('{book}',$text);

 
$tpl->compile('content');
$tpl->clear();
//-------------------------------------------------====
//    Вывод
//-------------------------------------------------====
/*echo book;*/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doktorpull, 2013-12-12
@doktorpull

Insert it between this one, right?

for($i=$first;$i<=$last;$i++)
{
if(@$filelink[$i]){@$list= "$filelink[$i]";}
 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question