Jump to content

get first 'line' of a block of html formatted text


kermy

Recommended Posts

I have some strings that contain fairly simply html code, for example:

 

"<p>This is the <b>first paragraph</b></p><p> this is the second</p>"

 

The tags are generally limited to things like <p>, <b>, <u>, <em> etc. I'd like to be able to 'preview' this string to the user by only showing the first line. In the above code, for example, '<p>This is the <b>first paragraph</b></p>'. If it was plain text,

 

I'd just use substr to get say the first 80 characters, but with the html formatting some of the characters will be hidden since they are mark-up. There's also the problem that the substring might not contain balanced tags, eg "<p>This is the <b>first paragraph<"

 

Any ideas how I'd do this? Perhaps the solution is not PHP, but somehow to use overlapping <div> to hide all the but first line?

Link to comment
Share on other sites

The real way to do that is to cut your 80 chars out, and look for all of the opening html tags, then add some closing tags in the reverse order that you found the opening tags.

 

Or if you know which tags you will find in the text like p, a, b, etc, and you don't mind cheating ( really bad!! ), you always add those closing tags at the end of your substring. Then if there weren't any tags in the 80 char substring, you would have some extra closing tags ( could be really bad !! )

 

 

 

I'm sure somebody else can suggest a quick regexp search that will find the tags for you, but here is an example that will search for all of the opening tags and then tack on some closing tags to the end of your 80 char substring. This example should work for most simple formatting tags, but if there are some '<'  or '>' characters that are not tags, it will probably make some mistakes

 

 

WARNING: Not tested. May contain an occasional bug.

<?php

// substring with unclosed tags
$haystack = '<p>This is the <a href="some_URL">first <b>paragraph';

$offset = 0;
$end_tags = "";
while( ($offset = stripos($haystack, '<', $offset)) !== false)
{

$tag_name_end = stripos($haystack, ' ', $offset);
$tag_end = stripos($haystack, '>', $offset);

if ($tag_name_end === false OR $tag_end === false)
{
  // Tag name is right at the 80 char border, so disregard it
  $haystack = substr($haystack, 0, $offset);
  break;
}

$offset++;
$tag_name = substr($haystack, $offset, $tag_name_end - $offset);
$end_tags .= "</" . $tag_name . ">";
$offset = $tag_end;
}
  
$haystack .= $end_tags;
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.