Solved

Truncate text by words

Posted on 2001-08-09
8
270 Views
Last Modified: 2008-03-10
I posted this question and got an acceptable answer. I then noticed that the solution produced no results when the sample contained few words than the amount sought in the truncation.

I am trying to truncate a text field by a certain number of words. The truncation needs to be word boundries. Here is the solution I got:

$pat = '~^(\S+\s+(?=\S)){4}~' ;
$sub = "hello, foo-bar!\nbaz quux whatever" ;
preg_match($pat, $sub, $match = array());
echo "'", trim(array_shift($match)), "'\n";

If the sample has more words than the number in the pattern (4 in this case) then this works beautifully. If the sample is shorter, it return nothing and I would like for it to return the original sample. I hope this makes sense.

Thanks for all your help.

0
Comment
Question by:td234
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Expert Comment

by:ykf2000
ID: 6370854
try this


$pat = '~^(\S+\s+(?=\S)){4}~' ;
$sub = "hello, foo-bar!\nbaz quux whatever" ;
if(strlen($sub) > 4)
{
  preg_match($pat, $sub, $match = array());
  $result =  "'", trim(array_shift($match)), "'\n";
}
else
{
  $result = $sub;
}

echo $result;
0
 
LVL 2

Author Comment

by:td234
ID: 6372465
Thanks ykf2000, but your solutions counts the characters, not the words. Your statement would return true because of the first 4 characters in "hello" and I need to know if it has 4 words or less.
0
 
LVL 4

Expert Comment

by:ykf2000
ID: 6372657
what about this:

$pat = '~^(\S+\s+(?=\S)){4}~' ;
$sub = "hello, foo-bar!\nbaz quux whatever" ;
preg_match($pat, $sub, $match = array());
if(strlen(trim(array_shift($match))) > 0)
{
 $result =  "'", trim(array_shift($match)), "'\n";
}
else
{
 $result = $sub;
}

echo $result;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:td234
ID: 6373393
This did not work as is, but was close. AS written, this returned the X (4th) word when the IF statement was true. I had to repeat the preg_match before the first result.
0
 

Expert Comment

by:netapi
ID: 6382238
0
 
LVL 2

Author Comment

by:td234
ID: 6384160
I am sure there is a cleaner way to do this than my modification to the above recomendation which has two regex's. Are there any regex experts out there with a solution?
0
 

Accepted Solution

by:
netapi earned 50 total points
ID: 6384879
Hmmm, I tried to post something before but it just says 'no text'... strange...

Anyway, I figured you could use {0,4} instead of {4} in your regular expression... that way you match at least 0 times, and at most 4 times, and that's what you want, right?

Maybe there's an easier solution using the 'split'-function, and a for-loop to show the first 4 elements of the resulting array, anybody care to comment on that?

Ciao,

netapi
0
 
LVL 2

Author Comment

by:td234
ID: 6384949
YES! That is the answer I was looking for. Thank you very much.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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…

718 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