Solved

tokenizing a string with max line length

Posted on 2012-04-02
2
406 Views
Last Modified: 2012-04-02
Hi, I would like to tokenize a string on word boundary with maximum line length of 80 characters. For some reason this does not work:

<?php


 function tokenTruncate($string) {
	  $matches = array();
      preg_match("/^(.{1,80})[\s]/i", $string, $matches);
 	return $matches;
}


$str = "aaaaaaaa aaaaaa aaaaaaaaa aaaaaa aaaaaa aaaaaaaa aaaaaa aaaaaaaaa aaaaaa aaaaaaa aaaaaa aaaaaaaa
bbbbbb bbbbbb bbbbbbb bbbbb bbbbbb bbbb bbbbb bbbb bbbbbbb bbbbbb bbbbb bbbbbbb bbbbbb bbbbbb bbbbb bbbbbbbb
ccccc cccccc cccccc cccccc ccccccc ccccc  cccccc cccccc ccccccc cccccccc ccccc cccccc ccccccc ccccccc
ddddd ddddddd ddddddd dddddd ddddddd  dddddd dddddd dddddd ddddd ddddddd dddd dddd ddddddddddddddd ";

  $tokens = array();
  $tokens  = tokenTruncate($str);

  print_r($tokens);
?>

Open in new window

0
Comment
Question by:Rozamunda
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37798599
Have you tried wordwrap() ?

<?php
$str = "aaaaaaaa aaaaaa aaaaaaaaa aaaaaa aaaaaa aaaaaaaa aaaaaa aaaaaaaaa aaaaaa aaaaaaa aaaaaa aaaaaaaa bbbbbb bbbbbb bbbbbbb bbbbb bbbbbb bbbb bbbbb bbbb bbbbbbb bbbbbb bbbbb bbbbbbb bbbbbb bbbbbb bbbbb bbbbbbbb ccccc cccccc cccccc cccccc ccccccc ccccc  cccccc cccccc ccccccc cccccccc ccccc cccccc ccccccc ccccccc ddddd ddddddd ddddddd dddddd ddddddd  dddddd dddddd dddddd ddddd ddddddd dddd dddd ddddddddddddddd ";

$tokens = explode("\n",wordwrap($str));

?>
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37798945
It sort of sounds like what you want is called a "teaser fragment" (a publishing term of art).  But the "tokenize" throws me off a bit.  See if this makes sense.
<?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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now