?
Solved

tokenizing a string with max line length

Posted on 2012-04-02
2
Medium Priority
?
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 2000 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 111

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

770 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