I need to display part of text from mysql:
for exapmle i`m having 50characters and then i just want to display just part of it like 20characters
in the first page and the remain including the first as a link to another page.
That when someone clicks the link the whole 50 charecters wil appear.
Help me in doing this please.

Dani AI

Generated

Quick note: the original runtime error was just a misspelt function name (as pointed out), which explains why the character-based snippet failed for . Beyond that simple fix, here are practical, low-risk improvements that address the follow-ups in this thread (for example 's request to cut by words rather than characters), plus security and encoding notes.

To return the first N words (does not cut mid-word and removes HTML tags):

function truncate_words($text, $maxWords = 20) {
    $clean = trim(strip_tags($text));
    $words = preg_split('/\s+/', $clean);
    if (count($words) <= $maxWords) return $clean;
    return implode(' ', array_slice($words, 0, $maxWords)) . '...';
}

If you need a fixed number of characters but must support UTF-8, use a multibyte-safe function (mbstring must be available):

$snippet = mb_substr($fullText, 0, 100, 'UTF-8'); // first 100 characters, UTF-8 safe

Server-side truncation is also possible via SQL:

SELECT id, LEFT(joketext, 100) AS snippet FROM comment;

Always escape user text when printing to HTML:

echo htmlspecialchars($snippet, ENT_QUOTES, 'UTF-8');

Practical workflow: list page shows the safe snippet and a link like view.php?id=123; the detail page uses a prepared query (mysqli or PDO) to fetch and display the full text. Do not pass the full text via GET. If your entries contain HTML you want to keep, use an HTML-aware truncator (DOMDocument or a library) so tags are not broken. Finally, move away from old mysql_* calls—use mysqli or PDO with prepared statements for security and forward compatibility.

Recommended Answers

All 8 Replies

Use substr to get the first 20 characters of the string:

$first=substr($content,0,20);
print $first;

In this example, $content is the original paragraph, 0 is the starting point of the content to be returned, and 20 is the number of characters to be returned.

<?php
include"config.php";

$result=@mysql_query('SELECT joketext,jokedate,name FROM comment');
$num_results=mysql_num_rows($result);
if( $num_results>0){


echo "<center><b><font color=blue size=3>There are <font color=red>$num_results</font> Comments</font></b></center>";
if(!$result){
exit('<p>Error perfoming query:' .
mysql_error() .'</p>');
}
//Diaplay the text of each joke in paragraph
while($row=mysql_fetch_array($result)){

$joketext=subtr($row,0,10);
$name=$row;
$date=$row;

echo"$joketext sent by:$name Date:$date";

?>
its is giving error:
Call to undefined function subtr() in C:\wamp\www\mimi.php on line 18

You missed the second s in substr: $joketext=subtr($row['joketext'],0,10); should be $joketext=substr($row['joketext'],0,10);

thankx alot it is working now))))

Use substr to get the first 20 characters of the string:

$first=substr($content,0,20);
print $first;

In this example, $content is the original paragraph, 0 is the starting point of the content to be returned, and 20 is the number of characters to be returned.

That's very handy to know, but I was wondering if you'd be able to tell me how you would instead of picking out characters, pick out actual words. For example:

"bah bah black sheep have you any wool, yes sir, yes sir, three bags full."

Instead of using substr (because each sentence will not have the same amount of characters) is there a function that will take full words, such as the first ten?

Thanks,

Anthony

u all done well

Hi,

I having the same problem with displaying text from msql.

ex: i have a file called language_pt.php,

inside i have a code <?=$aset[Lang_BRA]?> and at DB

<?
$HOME= "Home";
$LOGIN= "Entrar";
?>

Using that same code with the welcome.php(inside <?=$aset[Welcome]?>

it displays all text on the page, but with language appears blank.

I need help because i am not so good.

regards,

antwan1986, did you ever find an answer to your question?? I have the same problem and would really appreciate it if you could help me out. thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.