Solved

tokenizing a string with max line length

Posted on 2012-04-02
2
409 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 35

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

713 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