Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to ignore HTML content with tags using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 351
    Comment on it

    Hello Reader's If you want to make an function which truncate all the html contents and ignore them then you can uset the Sren Lvborg printTruncated function making it UTF-8 (Needs mbstring) function. It's example as below:-

    /* Truncate HTML, close opened tags
    *
    * @param int, maxlength of the string
    * @param string, html 
    * @return $html
    */
    
    function htmlTruncate($maxLength, $html)
    {
        mb_internal_encoding("UTF-8");
        $printedLength = 0;
        $position = 0;
        $tags = array();
        $out = "";

    while ($printedLength < $maxLength _ mb_preg_match('{</?([a-z]+)[^>]*>|_[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position)) { list($tag, $tagPosition) = $match[0];

    // Print text leading up to the tag. $str = mb_substr($html, $position, $tagPosition - $position); if ($printedLength + mb_strlen($str) > $maxLength) { $out .= mb_substr($str, 0, $maxLength - $printedLength); $printedLength = $maxLength; break; }

    $out .= $str; $printedLength += mb_strlen($str);

    if ($tag[0] == '&') { // Handle the entity. $out .= $tag; $printedLength++; } else { // Handle the tag. $tagName = $match[1][0]; if ($tag[1] == '/') { // This is a closing tag.

    $openingTag = array_pop($tags); assert($openingTag == $tagName); // check that tags are properly nested.

    $out .= $tag; } else if ($tag[mb_strlen($tag) - 2] == '/') { // Self-closing tag. $out .= $tag; } else { // Opening tag. $out .= $tag; $tags[] = $tagName; } }

    // Continue after the tag. $position = $tagPosition + mb_strlen($tag)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: