Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display line of text from text field

Status
Not open for further replies.

bill1one

Programmer
Sep 29, 2004
93
0
0
US
I am using the following code to display a few lines of text from a text field in my database.

Code:
echo substr($row['item_text'],0,256)

This works fine, cause at least it only shows only the first few lines of text, but really what I'd like to accomplish is only showing the lines surrounding the term (which is represented by the variable $term) I searched for in the MySQL query.

Any suggestions are appreciated.
 
If I'm understanding this correctly, then something like this might work for you:

Code:
echo substr($row['item_text'],(strpos($row['item_text'],$term)-100),256);



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Interesting.
Code:
substr($row['item_text'],-100,256)
AND
Code:
substr($row['item_text'],(strpos($row['item_text'],$term)-100),256);
produce the same results.
 
i know that this is a php forum but would not this be better done in mysql?

something like
Code:
$backTrack = 100; //number of chars to backtrack
$length = 200; ;// maximum length of string
$sql = "
SELECT SUBSTRING(item_text FROM (LOCATE(item_text, '$term') - $backTrack) FOR $length) from sometable where item_text like '%$term%' ";

 
That works too, but I'm getting the same results. Thanks for the suggestions. You've pointed me in the right direction.
 
You'll have to adjust the number of characters before and after the position of the term you looked for so you get different text.

If it gives you the same thing, its because there isn't enough text before the term to fill in the 100 characters. trying something small, like -5 instead. You should only get a smaller section of text around the term.

Code:
substr($row['item_text'],(strpos($row['item_text'],$term)-5),15);







----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top