Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

truncating a long title and wrapping to a new line

Posted on 2011-10-25
4
Medium Priority
?
227 Views
Last Modified: 2012-05-12
Hi all,

I have some text that I need to format that is fed from a db.

This text might be nicely structured with spaces or lots of characters connected e.g.:

wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

What I'm looking to do is apply some editing if a series of connected characters are detected without spacing.

So something like:

- If character length of string (which doesn't have any spaces) exceeds char limit, then add a <br /> so it wraps to a new line.

Cheers
0
Comment
Question by:dolythgoe
4 Comments
 
LVL 10

Assisted Solution

by:ukerandi
ukerandi earned 800 total points
ID: 37023506
Assuming your character limit is 12 then you can use like that
echo substr(fieldname,Strlen(fieldname)-12).”\n”. substr(fieldname,Strlen(fieldname)-12, Strlen(fieldname))
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 1200 total points
ID: 37023514
You can use wordwrap() for that.

<?php
$txt= "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww";
echo wordwrap($text, 20, "<br />", true);
?>
0
 

Author Comment

by:dolythgoe
ID: 37023578
Trust php to have a built in string function for this :) - Thanks guys
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37023587
PHP wordwrap() or substr() probably holds the solution you want - the devil is in the details.  In publishing we often use a thing called a "teaser fragment."
<?php // RAY_teaser_fragment.php
error_reporting(E_ALL);


// CREATE A TEASER FRAGMENT HEADLINE
// RETURN FIRST FEW WHOLE WORDS FOLLOWED BY ELLIPSES
// WITH A LINK TO THE FULL ARTICLE
// $length IS MINIMUM TRUNCATION CHARACTER COUNT


function teaser_fragment($text, $length=32, $url='/', $delim='|||')
{
    // IF TRUNCATION IS NEEDED
    if (strlen($text) > $length)
    {
        // IF TRUNCATION IS NEEDED, BREAK STRING APART
        $t = wordwrap($text, $length, $delim);
        $a = explode($delim, $t);
        $z = '...';
    }
    // IF TRUNCATION IS NOT NEEDED
    else
    {
        $a[0] = $text;
        $z = NULL;
    }

    // CONSTRUCT THE FRAGMENT WITH THE LINK AND ADD ELLIPSIS (LINK) TO THE END
    $teaser
    = '<a target="_blank" href="'
    . $url
    . '">'
    . $a[0]
    . $z
    . '</a>'
    ;
    return $teaser;
}



// USE CASES
echo "<pre>";
echo PHP_EOL;
echo "1...5...10...15...20...25...30...35...40...45..." . PHP_EOL;
echo teaser_fragment('Now is the time for all good men to come to the aid of their party');

echo PHP_EOL;
echo teaser_fragment('Now is the time for all good men to come to the aid of their party', 300);

echo PHP_EOL;
echo teaser_fragment('Now is the time for all good men to come to the aid of their party', 15, 'http://en.wikipedia.org/wiki/Filler_text');

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question