Jump to content
Larry Ullman's Book Forums

Html Text Handling With Substr()


Recommended Posts

I wonder if anyone can help with this little puzzle.

 

I am creating a listing of properties which includes a photo and a description in html.  

 

The description is drawn down from MySQL but may be 2,000 plus characters long.

 

As I only want to display the first, say, 300 characters and the <a> link (you know the sort of thing:

 

Wonderful property, hot and cold running water, great views....see more....

)

 

The code is:....

 

if($prop_live == "1")
{
echo '<tr style ="background-color: #FFFF99; border-collapse: separate; border-spacing: 12px 12px;"><td>' . $prop_name . '<br /><a href = "images/properties/' . $prop_ref . 'jpg" target = "_blank"><img src = "images/properties/' . $prop_ref . 'jpg" width = "160" alt = "Image missing - sorry." /></a></td><td style = "text-align: left; vertical-align: top; padding: 20px; ">' . substr($prop_desc, 0, 300)  . '  ...more information....<br /><br />' . $elec2 . '<br />' . $water2 . '</td><td style = "text-align: right; vertical-align: top;  padding: 20px;">' . $prop_price . '</td></tr>';  
}
(Note: I haven't added the <a href> bit yet around ...more information...)
 
All is well and good until we are unlucky enough to encounter an html control such as <sup> or á and cut it in half.  We then have incomplete html code which messes up the <td> or worse.  
 
I have scratched my head over this and been through all of the php text handling functions trying to cut the length of the string e.g. 303 or 298 characters or whatever, where one encounters a space.  The alternative would be to convert the html code <sup>2</sup> before 'echo'ing it.  I guess striptags would work but leave us with a non-superscript 2.  
 
Thanks
 
Max
Edited by Max
Link to comment
Share on other sites

Ah! This is a bit tricky. There are two issues, really:

1. Not breaking in a bad spot

2. Coping with the HTML

 

To solve the second, you can use strip_tags() to remove all HTML. If you're just using a snippet/preview, this may be feasible but you'd have to watch out for, say, links or what not. 

 

To solve the first, you can use stripos() to find the first occurrence of a safe breaking character--such as a space--after a certain point. Then you take a substring up until that point. 

 

Alternatively, you can take the short/preview description as another input (and database field) so you don't have to rely on magic and wouldn't have ugly results.

Link to comment
Share on other sites

Larry, what would we do without you???  :wub:

 

How about something like this??:

 

//We'll call the variable that contains the text '$my_text'

// html elements are not all the same size, eg <h1>, </strong>, <small>

 

// Do a 'while...'....then....

//Find the first opening tag (<)

$first = strpos("<")

 

//Find first closing tag (>)

$second = strpos('>',$my_text)

 

$length_of_string = $second - $first;

 

//Remove the offending element

 

substr_replace($my_text, "", $first, $length_of_string)

 

//end while

 

Unfortunately, this removes all of the HTML formatting.  

 

I have found this: 

html_entity_decode() Function 

 

which I'm going to have a play with to see how well it works - I'll report back.

 

Regards

 

Max

Link to comment
Share on other sites

  • 1 month later...

That's a great question, Larry.  

 

Actually, every other website that I have looked at just terminates the text irrespective of where there is a space.  e.g.  Lovely property, not overpriced at all, new ro..........more...

 

I have a bad habit of over-thinking these things.  All sorted now.

Link to comment
Share on other sites

 Share

×
×
  • Create New...