Here are several examples to illustrate the possibilities. Just understand the basic logic, and recombine according to the desired result!
Solutions with PHP:
<?php
$res2 = mysql_query("SELECT * FROM `entrada` WHERE `id_destino` =".$_GET['id']);
$row2 = mysql_fetch_array($res2);
$historia = $row2['historia'];
// Mostrar os primeiros 300 caracteres:
echo nl2br(
substr($historia, 0, 300)
);
// Mostrar o primeiro parágrafo com PHP 5.3 ou maior:
echo nl2br(
strstr($historia, chr(10), true);
);
// Mostrar o primeiro parágrafo:
echo nl2br(
substr($historia, 0, strpos($historia, chr(10) ) - 1);
);
// Mostrar o primeiro parágrafo, ou o próximo,
// se este tiver menos de 100 caracteres:
echo nl2br(
substr($historia, 0, strpos($historia, chr(10), 100 ) - 1);
);
// Mostrar cerca de 300 caracteres, mas sem quebrar palavras:
echo nl2br(
substr($historia, 0, strpos($historia, ' ', 300 ) - 1);
);
?>
Note: if you are going to use texts in UTF-8, give preferences to the functions multibyte mb_substr
and mb_strpos
Solutions with SQL:
$id = 0 + $_GET['id']; // SEU SCRIPT ORIGINAL É PORTA PARA SQL INJECTION!
// USE mysqli_ E BINDING NO LUGAR DE mysql_
//Primeiros 200 caracteres:
$query = "SELECT LEFT(`historia`,200) FROM `entrada` WHERE `id_destino` = $id"
//Primeiro paragrafo:
$query = "SELECT LEFT(`historia`,INSTR(`historia`,CHAR(10))-1) FROM `entrada` WHERE `id_destino` = $id"
//Cerca de 200 caracteres, quebrando entre palavras :
$query = "SELECT LEFT( `historia`, 199 + INSTR( SUBSTR( `historia`, 200 ),' ' ) ) FROM `entrada` WHERE `id_destino` = $id"
$res2 = mysql_query($query);
$row2 = mysql_fetch_array($res2);
echo nl2br($row2['historia']);
vc want to display for example the 200 first characters of the text or is it display only a random paragraph? Each paragraph is separated one by one
<br>
. If possible put a short example text.– rray
read the link: http://www.php.net/manual/en/function.explode.php and do so as I believe you will:
$itens = explode("<br />", nl2br($row2['historia']));
echo $itens[0];
– user6026