J.me

Limit string using regex with PHP

Sometime, we want to limit our string to display only few from the whole string. For example, in the homepage. This can be archieved easily using the default substr function. The example of the usage to limit to 25 characters:

<?php
    $string = “This is example string. Read it and forget it.”;
    echo substr($string, 0, 25);
?>

The result is

This is example string. R

Hmm… Not good… The string break in the middle of word and the string looks bad. Isn’t it better if we break it in the end of the word? So the result we want is

This is example string. Read

But how to do that? The answer is simply by using Regular Expression (regex). PHP provide two sets of regex functions, the POSIX Extended Regular Expressions and PCRE (Perl-Compatible Regular Expression). In this article, we will use the PCRE one.

The regex we will use is this

/^(.{1,25}[^\s]*).*$/s

Let’s see it in action

<?php
    $string = “This is example string. Read it and forget it.”;
    $reg = “/^(.{1,25}[^\s]*).*$/s”;
    echo preg_replace($reg, “\\1”, $string);
?>

The result is

This is example string. Read

Yeah, that’s it! And now a little description of how it works.

First the both / (slash) in the begining and end is the default syntax for Perl regex. We can however, change it to another unique character.  For example @. The finishing s means it needs to also match newline character. Now let’s take a look on the regex, we begin with ^ which means we want to match from the start of string. The (.{1,25}[^\s]*) do the trick. The enclosed brackets will group this matched string, then we can later use it using \\1. Now see the .{1,25}, this will match any characters from 1 to 25, so it stop match in the 25th characters. The [^\s]* will match any character except whitespace. So after we match until the 25th character, it then match any characters until the whitespace occured, which means, until the end of the word. 🙂 Now the rest .*$ just to match until the end of string, so when we use preg_replace, we can get the first group and delete the rest.

Hmm… But isn’t it better if we create our own function for this? Yep, here it is.

<?php
function limit_the_string($string, $limit)
{
    if (strlen($string) < $limit) // if the limit is more than the string length, we will do nothing and return the whole string
        return $string;
    $reg =”/^(.{1,” . $limit . “}[^\s]*).*$/s”;
    return preg_replace($reg, ‘\\1’, $string);
}
?>

And the usage

<?php
    $string = “This is example string. Read it and forget it.”;
    echo limit_the_string($string, 25);
?>

Well done… Thanks for reading. 🙂

10 comments | Leave a comment

  1. Philip Petrov March 9, 2009 18:12

    Very good. You did a nice tutorial here!

  2. Sam Benson April 19, 2009 16:29

    Regex makes my brain melt – this was really handy. Thanks!

  3. ghprod May 8, 2009 00:10

    great tutorial … simple and full with function:)

    thnx

    regards

  4. keaglez May 10, 2009 03:32

    Glad if it useful for you. 🙂

  5. George Garchagudashvili July 14, 2009 19:56

    Hey very nice ya, but what about if we need to take 4 digits from the string and not matter from what part
    $string = “dfd4534565465fher5645g”;
    $pattern = “/(\d{1,5}[^\s]{0}).*$/”;
    echo preg_replace($pattern,”\\1″,$string);
    i got 5 digits but also first ‘dfd’ from the string, i hope youcan help me.
    Thanks

  6. keaglez July 15, 2009 13:38

    do you mean to take the first 4 digit numbers from string? if that so, then try this pattern

    $pattern = “/^[^0-9]*(\d{1,4}).*$/”;

    It needs to match from the beginning of string, so we can extract the needed part with preg_replace. We can, however, use preg_match and get required string using reference variable that passed into the third parameter. For example:

    $string = “dfd4534565465fher5645g”;
    $pattern = “/(\d{1,4})/”;
    preg_match($pattern, $string, $match);
    echo $match[1];

    This is far easier, since regex search from beginning, we only need to look for 4 digits, and it will took the first 4 digits it matched… 🙂 Hope it useful for you… 😉

  7. rangga May 29, 2010 20:01

    thanks gan 🙂

  8. andre December 9, 2010 10:22

    nice info.. it helps me alot. thx again

  9. karose January 20, 2013 09:28

    good

Leave a comment