Home » PHP/MySQL, Programming Techniques » PHP find substring without breaking words or don’t cut words – MultiByte (MB) Strings

0

Often we need to extract a portion of a string specified by a length. When we use the PHP function substr to fetch a part of the string, we can only specify the length of the new string. That means, PHP will cut the string at the specified length and will not worry about words. When you usesubstr for showing excerpts, it doesn’t look good with a split word at the end. Use the below code to extract part of a string for a specified length. The example uses the PHP MultiByte functions so you can use it for multi-byte encoded languages. If you are going to use it only for English, just replace all mb_ functions with normal functions.

<?php
mb_internal_encoding("UTF-8");
$str = 'YOUR LONG STRING';
$limit = 100; //Specify the length of the new substring
if(substr($str, $limit, 1) != ' ' && ($l = mb_strrpos(mb_substr($str, 0, $limit), ' '))) {
	echo mb_substr($str, 0, $l);
} else {
	echo mb_substr($str, 0, $limit);
}
?>

If the character at the length specified is not a space, the code tries to find the previous space and cuts the string from there. If there is no space in the whole string it cuts at the specified length. Even though it uses a few mb_ functions, the performance was great. The execution time was very low and negligible (0.0003~).

You can also use the wordwrap function to achieve the same.

<?php
$length = 50;
$str = 'YOUR LONG STRING';
$arr = explode('__BREAK__', wordwrap($str, $length, "__BREAK__", true));
echo $arr[0];
?>

Leave a Reply

Page optimized by WP Minify WordPress Plugin