Solved

tokenizing a string with max line length

Posted on 2012-04-02
2
408 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 109

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
The viewer will learn how to dynamically set the form action using jQuery.
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…

791 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