note 87533 modified in function.wordwrap by danbrown

From: Date: Thu, 18 Dec 2008 14:41:13 +0000
Subject: note 87533 modified in function.wordwrap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148311@lists.php.net to get a copy of this message
<?php function substr_word($str, $start, $end) { //function taking sub string from $str in between $start chars and $end chars. $String = wordwrap($str, ($end - $start), '[cut]', false); //add [cut] for each word after $end chars $ExplodedString = explode('[cut]', $String); //make an array with elements long for $end chars $end=ceil(strlen($text)/$end); //how many elements of array are there. (celi > round >floor) $i=floor($end/$start); //take an element with start position $String = $ExplodedString[$i]; if ($String[0]==" ") { substr($String, 1); } if ($String[0]==" ") { substr($String, 1); } if ($String[0]==" ") { substr($String, 1); } return $String; } ?> Usage example: <?php $from=strpos($text,$word)-100; $to=strpos($text,$word)+400; $text=substrpos($text, $from, $to); ?> will return text 100 chars before and 400 chars after searched word without breaking the words in text. Good for seach scirpts for your website - imagine that $word is keyword you search for. [NOTE BY danbrown AT php DOT net: Contains a fix provided by (admin AT studio-gepard DOT pl).] --was-- <?php function substr_word($str, $start, $end) { //function taking sub string from $str in between $start chars and $end chars. $String = wordwrap($str, $end, '[cut]', false); //add [cut] for each word after $end chars $ExplodedString = explode('[cut]', $String); //make an array with elements long for $end chars $end=ceil(strlen($text)/$end); //how many elements of array are there. (celi > round >floor) $i=floor($end/$start); //take an element with start position $String = $ExplodedString[$i]; if ($String[0]==" ") { substr($String, 1); } if ($String[0]==" ") { substr($String, 1); } if ($String[0]==" ") { substr($String, 1); } return $String; } ?> Usage example: <?php $from=strpos($text,$word)-100; $to=strpos($text,$word)+400; $text=substrpos($text, $from, $to); ?> will return text 100 chars before and 400 chars after searched word without breaking the words in text. Good for seach scirpts for your website - imagine that $word is keyword you search for. http://php.net/manual/en/function.wordwrap.php

« previous php.notes (#148311) next »