Hi frnds....
i have a colmn in database like CONTENT ..it contains 20 lines of of data....but i need to select only 5 lines of data to a single variable......
plz tell me how can i do?
Hi frnds....
i have a colmn in database like CONTENT ..it contains 20 lines of of data....but i need to select only 5 lines of data to a single variable......
plz tell me how can i do?
The thread shows two common issues: mixed delimiters in the stored text and PHP string quoting. correctly pointed out a server-side string function can return text up to the nth delimiter (works only when the column actually contains newline characters). The original parse error happened because double quotes were nested inside a double-quoted PHP string; using single quotes or escaping fixes that. ’s nl2br()+explode() approach works once the DB value has HTML <br /> tags, but it’s brittle (different browsers/filters produce "<br>", "<br />", or paragraph tags).
A more robust approach is to handle splitting in PHP and support both plain-text newlines and HTML. For plain text (CRLF/LF/CR), use a regex split and array_slice:
$raw = $row['content']; // fetched via PDO/mysqli
$lines = preg_split('/\r\n|\n|\r/', $raw);
$first5 = implode("\n", array_slice($lines, 0, 5)); If the DB stores HTML paragraphs, parse the HTML and extract the first N <p> elements to preserve markup and avoid fragile string matching:
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML('<div>'.$raw.'</div>');
$ps = $doc->getElementsByTagName('p');
$parts = [];
for ($i = 0; $i < min(5, $ps->length); $i++) {
$parts[] = $doc->saveHTML($ps->item($i));
}
$snippet = implode('', $parts); Notes and cautions: inspect the raw DB value with a SQL client to confirm whether newlines, <br>, or <p> were stored. If splitting on BR tags, use a regex like '/<br\s\/?>/i' to match variants. Avoid deprecated mysql_ functions—use mysqli or PDO with prepared statements. When outputting plain text, escape it (htmlspecialchars) to prevent XSS. These steps make the extraction reliable across different input sources.
— nav33n 472Jump to Post
Jump to Post— nav33n 472Use single quote in your query ' instead of ". The second argument \n is a delimiter. If you are storing the values in a table with <br> try giving that instead of \n. I tested at my localhost and it works without any problem.
<?php $con …
Jump to Post— nav33n 472As I said in my earlier post, \n is just a delimiter. How do you know it contains 20 lines of of data ? How are you separating the lines ? Using \n or by using <br> ?
Try this simple example.$query = "select content from …
Check mysql function substring_index
Eg.
SELECT SUBSTRING_INDEX(columnname, "\n", 10 ) FROM table
hi Naveen..
$blog="select sno,title,name,SUBSTRING_INDEX(content, "\n", 5 ) from blogs limit 1";
when i using the above query i got this error..plz resolve this one...
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in ....
and
Parse error: syntax error, unexpected T_STRING in....
Use single quote in your query ' instead of ". The second argument \n is a delimiter. If you are storing the values in a table with <br> try giving that instead of \n. I tested at my localhost and it works without any problem.
<?php
$con = mysql_connect("localhost","root");
mysql_select_db("test");
$blog="select id,SUBSTRING_INDEX(question, '\n', 5 ) from quiz_questions limit 1";
$result = mysql_query($blog) or die (mysql_error());
$row = mysql_fetch_array($result,MYSQL_ASSOC);
print "<pre>";
print_r($row);
print "</pre>";
?> :) Get a good editor with syntax highlighting.. That way you will know where you are going wrong!
Use single quote in your query ' instead of ". The second argument \n is a delimiter. If you are storing the values in a table with <br> try giving that instead of \n. I tested at my localhost and it works without any problem.
<?php $con = mysql_connect("localhost","root"); mysql_select_db("test"); $blog="select id,SUBSTRING_INDEX(question, '\n', 5 ) from quiz_questions limit 1"; $result = mysql_query($blog) or die (mysql_error()); $row = mysql_fetch_array($result,MYSQL_ASSOC); print "<pre>"; print_r($row); print "</pre>"; ?>:) Get a good editor with syntax highlighting.. That way you will know where you are going wrong!
Hi Naveen...Thank U 4 giving reply...But,my problem not solved...may its my mistake..plz check this one...
<?php
$blog="select sno,title,name,SUBSTRING_INDEX(content, '\n', 5 ) from blogs limit 1";
$brow=mysql_query($blog) or die(mysql_error());
$bres=mysql_fetch_array($brow);
$bid=$bres['sno'];
$btitle=$bres['title'];
$bname=$bres['name'];
$bcontent=$bres['content'];
?> here i got everything.. but the content is NULL....
and also in ur query execution what is MYSQL_ASSOC ?
As I said in my earlier post, \n is just a delimiter. How do you know it contains 20 lines of of data ? How are you separating the lines ? Using \n or by using <br> ?
Try this simple example.
$query = "select content from blogs limit 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
print htmlentities($row['content']);
?> If the lines are separated by <br> tag, use that as a delimiter.
Btw, I use mysql_assoc to get only the associative indices. I can use mysql_assoc instead, but I like using mysql_fetch_array. :)
Check Return Values in this link for more details.
http://in.php.net/mysql_fetch_array
As I said in my earlier post, \n is just a delimiter. How do you know it contains 20 lines of of data ? How are you separating the lines ? Using \n or by using <br> ?
Try this simple example.$query = "select content from blogs limit 1"; $result = mysql_query($query); $row = mysql_fetch_array($result,MYSQL_ASSOC); print htmlentities($row['content']); ?>If the lines are separated by <br> tag, use that as a delimiter.
Btw, I use mysql_assoc to get only the associative indices. I can use mysql_assoc instead, but I like using mysql_fetch_array. :)
Check Return Values in this link for more details.
http://in.php.net/mysql_fetch_array
Hi Naveen....
here when i inserting data from a text area as copy and paste..It looks like paragraphs...when i echo the total content in my web page...i saw the source code, then there is no <br>...it looks totally as paragraphs....
Sry 4 distrubing u more times..
Then use <p> instead :-/ It would be very helpful if you show us the output !
Then use <p> instead :-/ It would be very helpful if you show us the output !
Hi Naveen...
Thank U Sooo much....
Finally I got it....by using this way....
$bcontent=nl2br($bres['content']);
$bc=explode("<br />",$bcontent);
echo $bc['0']; Once Again Thank U.....
Great :) Congrats!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.