开发者

Create a blurb from text from a database (similar to "read more")

开发者 https://www.devze.com 2022-12-10 10:12 出处:网络
I am trying to cut text from a database off if <!-- break --> is found then only show what is before the break. I currently have this code

I am trying to cut text from a database off if <!-- break --> is found then only show what is before the break. I currently have this code

        //get the description before the more link
        $project_blurb =  htmlspecialchars_decode($project_data['p_desc']);
        if (strstr($project_blurb, '<!-- break -->')) {
            $project_blurb = explode(开发者_如何学Python"<!-- break -->" , $project_blurb);
            $project_desc = $project_blurb[0];
            }
        else{
            $project_desc = $project_blurb;
        }

This works except that the text starts with a <p> tag and currently I'm cutting it off the ending </p> tag which is breaking the html on the site. I want to know if there is a better way to only get the description before the "break". I've tried to us strip_tags but this also strips the comment that I need to search for and creates some ugly formatting.

Thanks


A solution I came up with is similar to jheddings method.

I corrected his script up and used a code snippet I found here

Snipplr Close Tags In A HTML Snippet

To find open tags and close them (Note that I am assuming you really only care about closing p tags)

Note: The snippet may have shortcomings but it managed to get the job done for the example I was working with

So in the example script below I am taking the sample blurb cutting out everything after the break marker and appending "..." to it. Then we strip_tag everything except the p tags. Then I am using the closetags function to match all tags and close any that are unmatched.

It is far from neat but if your data set is simple enough it may be a quick way to go about it.

<?php
$project_blurb = "<p>This is a blurb with content</p><p>This is another<!-- break -->blurb</p>"; 

if ($pos = strpos($project_blurb, '<!-- break -->')) {
    $project_desc = substr($project_blurb, 0, $pos)."...";
} else {
    $project_desc = $project_blurb;
}

$project_desc = strip_tags($project_desc, '<p>');
$project_desc = closetags($project_desc);

echo $project_desc;

function closetags ( $html )
{
    #put all opened tags into an array
    preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
    $openedtags = $result[1];

    #put all closed tags into an array
    preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
    $closedtags = $result[1];
    $len_opened = count ( $openedtags );
    # all tags are closed
    if( count ( $closedtags ) == $len_opened )
    {
        return $html;
    }
    $openedtags = array_reverse ( $openedtags );
    # close tags
    for( $i = 0; $i < $len_opened; $i++ )
    {
        if ( !in_array ( $openedtags[$i], $closedtags ) )
        {
            $html .= "</" . $openedtags[$i] . ">";
        }
        else
        {
            unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
        }
    }
    return $html;
}

?>


You guys are all doing it wrong!!

It's coming fromt he DB so this is how i do it:

SELECT SUBSTR(description, 1, INSTR(description, '<!-- break -->') -1);

Why make it overtly difficult w/ programming languages???

Demo!!

CREATE TEMPORARY TABLE foo (description text);
INSERT INTO foo VALUES ('This is a really long paragraph. <!-- break --> OK, not really ;-)');
SELECT SUBSTRING(description, 1, INSTR(description, '<!-- break -->') - 1) AS description FROM foo;
// Output: This is a really long paragraph.


If you need to select up to your break, call strip tags after you get the field. Also, you can make it a little more efficient by only searching for your break once:

<?
$project_blurb = htmlspecialchars_decode($project_data['p_desc']);
if (($pos = strpos('<!-- break -->'), $project_blurb) >= 0) {
  $project_desc = substr(0, $pos);
} else {
  $project_desc = $project_blurb;
}
$project_desc = strip_tags($project_desc);
?>

Instead of putting a special comment in your text, consider defining a maximum length for your description, so you don't have to search the string. This will be much more efficient and won't require you to modify your input.

If you are going to link to the full-text from your page, instead of expanding it in place, you could also consider letting your database do some of the work for you. Assuming you are using MySQL, you could use LEFT(p_desc, 200) to pull only the first 200 characters of the database from the p_desc field.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号