Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

PHP word wrapping

There is a built in function wordwrap, that can divide long text into multiple lines by specified character amount, but sometimes that's just not enough. There are situations when you need more custom approach to divide text into multiple lines.

For example, when dealing with UTF-8, you should use multi-byte safe functions, plus you might not want to cut words in half, but break lines on spaces if possible.

Here is a function to do it:
<?php
/**
 * Multibyte capable wordwrap
 *
 * @param string $str
 * @param int $width
 * @param string $break
 * @return string
 */
function mb_wordwrap($str, $width=75, $break="rn")
{
    // Return short or empty strings untouched
    if(empty($str) || mb_strlen($str, 'UTF-8') <= $width)
        return $str;
   
    $br_width  = mb_strlen($break, 'UTF-8');
    $str_width = mb_strlen($str, 'UTF-8');
    $return = '';
    $last_space = false;
    
    for($i=0, $count=0; $i < $str_width; $i++, $count++)
    {
        // If we're at a break
        if (mb_substr($str, $i, $br_width, 'UTF-8') == $break)
        {
            $count = 0;
            $return .= mb_substr($str, $i, $br_width, 'UTF-8');
            $i += $br_width - 1;
            continue;
        }

        // Keep a track of the most recent possible break point
        if(mb_substr($str, $i, 1, 'UTF-8') == " ")
        {
            $last_space = $i;
        }

        // It's time to wrap
        if ($count >= $width)
        {
            // There are no spaces to break on!  Going to truncate :(
            if(!$last_space)
            {
                $return .= $break;
                $count = 0;
            }
            else
            {
                // Work out how far back the last space was
                $drop = $i - $last_space;

                // Cutting zero chars results in an empty string, so don't do that
                if($drop > 0)
                {
                    $return = mb_substr($return, 0, -$drop);
                }
                
                // Add a break
                $return .= $break;

                // Update pointers
                $i = $last_space + ($br_width - 1);
                $last_space = false;
                $count = 0;
            }
        }

        // Add character from the input string to the output
        $return .= mb_substr($str, $i, 1, 'UTF-8');
    }
    return $return;
}
?>

Other thing you might need is to ignore html or xml tags and wrap only contents of the tag, for example not to break too long url, but break only text between <a></a> tags:

<?php
function tagwrap($html, $width = 75, $break = "nr") 
{
    //using dom object
    $dom = new domDocument;

    //load the html into the object
    $dom->loadXML($html);

    // preserve white space
    $dom->preserveWhiteSpace = true;

    //getting all tags
    $content = $dom->getElementsByTagname("*");
    
    $html = "";
    foreach ($content as $item)
    {
        //wrapping contents of tags, function described above is used, 
        //but you can use your own function or simple wordwrap() php function
        $item->nodeValue = mb_wordwrap($item->nodeValue, $width, $break);
        $html .= $dom->saveXML($item);
    }
    //return the results 
    return $html;
}
//link is not broken by lines, only text inside tags
echo tagwrap("<a href='aaaaaa'>aaaaaa</a>", 2);

?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus