Solved

tokenizing a string with max line length

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
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…
The viewer will learn how to dynamically set the form action using jQuery.

688 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