note 87116 added to ref.strings

From: Date: Thu, 20 Nov 2008 01:33:07 +0000
Subject: note 87116 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-146733@lists.php.net to get a copy of this message
If you want a function to return all text in a string up to the Nth occurrence of a substring, try the below function. Works in PHP >= 5. (Pommef provided another sample function for this purpose below, but I believe it is incorrect.) <?php // Returns all of $haystack up to (but excluding) the $n_occurrence occurrence of $needle. Therefore: // If there are < $n_occurrence occurrences of $needle in $haystack, the entire string will be returned. // If there are >= $n_occurrence occurrences of $needle in $haystack, the returned string will end before the $n_occurrence'th needle. // This function only makes sense for $n_occurrence >= 1 function nsubstr($needle, $haystack, $n_occurrence) { // After exploding by $needle, every entry in $arr except (possibly) part of the last entry should have its content returned. $arr = explode($needle,$haystack,$n_occurrence); // Examine last entry in $arr. If it contains $needle, cut out all text except for the text before $needle. $last = count($arr) - 1; $pos_in_last = strpos($arr[$last],$needle); if ($pos_in_last !== false) $arr[$last] = substr($arr[$last],0,$pos_in_last); return implode($needle,$arr); } $string = 'd24jkdslgjldk2424jgklsjg24jskgldjk24'; print 'S: ' . $string . '<br>'; print '1: ' . nsubstr('24',$string,1) . '<br>'; print '2: ' . nsubstr('24',$string,2) . '<br>'; print '3: ' . nsubstr('24',$string,3) . '<br>'; print '4: ' . nsubstr('24',$string,4) . '<br>'; print '5: ' . nsubstr('24',$string,5) . '<br>'; print '6: ' . nsubstr('24',$string,6) . '<br>'; print '7: ' . nsubstr('24',$string,7) . '<br>'; /* // prints: S: d24jkdslgjldk2424jgklsjg24jskgldjk24 1: d 2: d24jkdslgjldk 3: d24jkdslgjldk24 4: d24jkdslgjldk2424jgklsjg 5: d24jkdslgjldk2424jgklsjg24jskgldjk 6: d24jkdslgjldk2424jgklsjg24jskgldjk24 7: d24jkdslgjldk2424jgklsjg24jskgldjk24 */ ?> Note that this function can be combined with wordwrap() to accomplish a routine but fairly difficult web design goal, namely, limiting inline HTML text to a certain number of lines. wordwrap() can break your string using <br>, and then you can use this function to only return text up to the N'th <br>. You will still have to make a conservative guess of the max number of characters per line with wordwrap(), but you can be more precise than if you were simply truncating a multiple-line string with substr(). See example: <?php $text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque id massa. Duis sollicitudin ipsum vel diam. Aliquam pulvinar sagittis felis. Nullam hendrerit semper elit. Donec convallis mollis risus. Cras blandit mollis turpis. Vivamus facilisis, sapien at tincidunt accumsan, arcu dolor suscipit sem, tristique convallis ante ante id diam. Curabitur mollis, lacus vel gravida accumsan, enim quam condimentum est, vitae rutrum neque magna ac enim.'; $wrapped_text = wordwrap($text,100,'<br>',true); $three_lines = nsubstr('<br>',$wrapped_text,3); print '<br><br>' . $three_lines; $four_lines = nsubstr('<br>',$wrapped_text,4); print '<br><br>' . $four_lines; /* prints: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque id massa. Duis sollicitudin ipsum vel diam. Aliquam pulvinar sagittis felis. Nullam hendrerit semper elit. Donec convallis mollis risus. Cras blandit mollis turpis. Vivamus facilisis, sapien at tincidunt accumsan, arcu Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque id massa. Duis sollicitudin ipsum vel diam. Aliquam pulvinar sagittis felis. Nullam hendrerit semper elit. Donec convallis mollis risus. Cras blandit mollis turpis. Vivamus facilisis, sapien at tincidunt accumsan, arcu dolor suscipit sem, tristique convallis ante ante id diam. Curabitur mollis, lacus vel gravida */ ?> ---- Server IP: 208.69.120.35 Probable Submitter: 192.76.177.124 ---- Manual Page -- http://www.php.net/manual/en/ref.strings.php Edit -- https://master.php.net/note/edit/87116 Del: integrated -- https://master.php.net/note/delete/87116/integrated Del: useless -- https://master.php.net/note/delete/87116/useless Del: bad code -- https://master.php.net/note/delete/87116/bad+code Del: spam -- https://master.php.net/note/delete/87116/spam Del: non-english -- https://master.php.net/note/delete/87116/non-english Del: in docs -- https://master.php.net/note/delete/87116/in+docs Del: other reasons-- https://master.php.net/note/delete/87116 Reject -- https://master.php.net/note/reject/87116 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146733) next »