• t
  • g
  • B
  • Z
  • @
  • e

portfolio & blog of senior web developer, Fahd Murtaza



  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

  • About
  • Portfolio
  • WordPress
  • CV
  • Blog
  • Contact

PHP: Text cropping with defining the number of characters, words or sentences

March 18th, 2009 by Fahd

I have recently been working on PHP RSS reader script that was supposed to do three different things with RSS description text. To make a teaser RSS text, I was asked to develop the functionality in a such a way that text can be cropped in these three ways.

  • with defining the number of words.
  • with defining the numbers of characters.
  • with defining the number of sentences.

Here is a set of functions that helps you crop a text in three different ways. The usage of the function is also mentioned at the end f the script.
<?php

/**
* cropsentence()
*
* @param mixed $str
* @param mixed $noof
* @param integer $mode
// 0 means crop by no of characters // 1 means crop by no of sentences // 2 means crop by number of words
* @param string $appendwith
* @return
*/
function cropsentence($str, $noof, $mode = 0, $appendwith = “…”)
{
if (
strlen($str) > $chars) {
if (
$mode == 0) { // 0 means crop by no of characters
$str = substr($str, 0, $noof);
} elseif (
$mode == 1) { // 1 means crop by no of sentences
$str = getLeadingSentences($str, $noof);
} elseif (
$mode == 2) { // 2 means crop by number of words
$str = wordTrim($str, $noof);

} elseif ($mode < 0 || $mode > 2) { // others means do nothing
echo “The current mode is not correct.”;
}
$str = $str . $appendwith;
return
$str;
} else {
return
$str;
}
}

//getLeadingSentences
//Copyright (c) 2000 Jason R. Pitoniak.  All rights reserved.
//jason@interbrite.com http://www.interbrite.com

//If you find this code useful, find a bug, or have a suggestion,
//please email me.  Feel free to use this code for any purpose.

/**
* getLeadingSentences()
*
* @param mixed $data
* @param mixed $max
* @return
*/
function getLeadingSentences($data, $max)
{
//given string $data, will return the first $max sentences in that string

//in: $data = the string to parse, $max = maximum # of sentences to return
//returns: string containing the first $max sentences
//(If the # of sentences in the string is less than $max,
//then entire string will be returned.)

//a sentence is any charactors except ., !, and ?
//any number of times,  plus one or more .s, ?s, or !s
//and any leading or trailing whitespace:
$re = “^s*[^.?!]+[.?!]+s*”;
$out = “”;
for (
$i = 0; $i < $max; $i++) {
if (
ereg($re, $data, $match)) {
//if a sentence is found, take it out of $data and add it to $out
$out .= $match[0];
$data = ereg_replace($re, “”, $data);
} else {
$i = $max;
}
}
return
$out;
}

/**
* getLeadingWords()
*
* @param mixed $data
* @param mixed $max
* @return
*/
function getLeadingWords($data, $max)
{
//given string $data, will return the first $max sentences in that string

//in: $data = the string to parse, $max = maximum # of sentences to return
//returns: string containing the first $max sentences
//(If the # of sentences in the string is less than $max,
//then entire string will be returned.)

//a sentence is any charactors except ., !, and ?
//any number of times,  plus one or more .s, ?s, or !s
//and any leading or trailing whitespace:
$re = “^s*[^.?!]+[.?!]+s*”;
$out = “”;
for (
$i = 0; $i < $max; $i++) {
if (
ereg($re, $data, $match)) {
//if a sentence is found, take it out of $data and add it to $out
$out .= $match[0];
$data = ereg_replace($re, “”, $data);
} else {
$i = $max;
}
}
return
$out;
}

/**
* wordTrim()
*
* @param mixed $str
* @param mixed $len
* @return
*/
function wordTrim($str, $len)
{
$wordCount = 0;
$charCount = 0;

$length = strlen(strip_tags($str));
for (
$i = 0; $i < $length; $i++) {
if (
$str[$i] == ‘ ’) {
$wordCount++;
$charCount++;
if (
$wordCount == $len)
break;
} else {
$charCount++;
}
# end if
} # end for loop

$newstr = substr($str, 0, $charCount);
return
$newstr;
}
# end function

# Example used for testing purpose

$str2 = “Hello there I work for you. I have been working for other guys, but you are the coolest guy among them.
Also I really like the way you guide me with new technologies and the latest trends. You always tell me to keep working hard!”
;
echo
cropsentence($str2, 22, 2);

?>

Usage

cropsentence($str, $noof, $mode, $appendwith );

$str: This is the string / text to be cropped

$noof: Number of characters/words/sentences based on the $mode parameter

$mode: Three values of $mode can be defined

1. 0 means crop by no of characters.
2. 1 means crop by no of sentences .
3. 2 means crop by number of words.

$appendwith: This can be any characters like ‘…’ , ‘…more’ etc which will be appended with the cropped text when its returned.

Download:

You can download the script here.

Posted in PHP, PHP Tips, Web Development

5 Comments

  1. Jason Pitoniak
    Posted April 3, 2009 at 5:06 pm | Permalink

    I was just searching Google for “getLeadingSentences,” as I do every once in a while, to see where my code has ended up and I came across this blog. I’m glad that you found that function, that I wrote ages ago, useful.

    Thank you also for leaving that attribution intact. I’ve always believed in giving credit where credit is due, so I appreciate it when others do the same for my work. I’ve seen that function in other places with all of the comments removed and “stolen from somewhere” added to the top.

    –Jason

  2. Fahd Murtaza
    Posted April 3, 2009 at 5:13 pm | Permalink

    Yes Jason

    Its great that you shared it with the community. Its always great to share your code with others. It definitely saved my time. I ended up doing a way cooler thing with it which could have taken more time without using your code. I am mailing you the final output I delivered to the client. Its a XML (RSS) parser that had above features as well.

    -Fahd

  3. Kawish anjum Swati
    Posted October 19, 2009 at 10:07 am | Permalink

    Fahad bhai would we live be friendz?
    My cell,
    00923453790448
    00923133790448,
    Em@il,
    kawish_loveconnection@yahoo.com
    awarapan_0008@yahoo.com,
    Kawish anjum swati;
    19 october 2009
    thanks

  4. Elvis
    Posted October 18, 2011 at 5:47 pm | Permalink

    Hi yea,
    The black background of your site makes it impossible to read your content. Your site is not fit for purpose. Pity

  5. Guillermo
    Posted November 8, 2011 at 1:36 am | Permalink

    This is great. I always wanted this, so i copied the text into a static class and added self:: where needed. However, i’m not sure why you included the $chars var. Is it just to avoid cropping when string is too short?

    if (strlen($str) > $chars) {
    }

    I think i’ll take ride of that.

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Categories


Want to Hire Me?


Fahd Murtaza is the guy behind fahdmurtaza.com: your source for getting your web development projects done. Fahd sepcialises in Open Source Web Development and his favorite is WordPress. This site is a platform to his web development portfolio and blog all on a newly designed responsive website—try viewing it on your mobile.

If after viewing Fahd's portfolio, you think he might be just the guy to hire for your web / application development; check out the web development pricing page, then find out how to hire Fahd with a web development project idea and a brief of requirements.

 


Fahd Murtaza has put together a number of Web Development Articles that will help you see what he does, how he does it. You will also be able to see the level of work, and passion, that Fahad puts into each and every job. Understanding a little about the process can make the idea of investing good money much more palatable. People have always appreciated my Agile techniques. Its time you

Fahd Murtaza, Web Developer, Programmer, Wordpress Expert
U
This is portfolio & blog of senior web developer Fahd Murtaza, who has 9 years experience in: website development, WordPress, drupal, CMS and CRM application development with passion of making web better; one site at a time.


@
Mobile +968 93 678 199
email info@fahdmurtaza.com
Google Talk: fahdim@gmail.com
Skype fahd.murtaza
Location Muscat, Oman.


_
Developed using my beloved, love of my life, WordPress, built on the responsive, grid based, mobile optimised, Foundation Framework, and a modified Foundation theme. More Info →


Follow Fahd: Twitter / Google+ / Instagram / Facebook / Dribbble / Tumblr / Posterous/ flickr /
Copyright © 2006-2012 Fahd Murtaza

    • WordPress.org
    • Documentation
    • Support Forums
    • Feedback