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.)
// 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 >= 1functionnsubstr($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);
returnimplode($needle,$arr);
}$string='d24jkdslgjldk2424jgklsjg24jskgldjk24';
print'S: '.$string.'
';
print'1: '.nsubstr('24',$string,1) .'
';
print'2: '.nsubstr('24',$string,2) .'
';
print'3: '.nsubstr('24',$string,3) .'
';
print'4: '.nsubstr('24',$string,4) .'
';
print'5: '.nsubstr('24',$string,5) .'
';
print'6: '.nsubstr('24',$string,6) .'
';
print'7: '.nsubstr('24',$string,7) .'
';/*
// 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
, and then you can use this function to only return text up to the N'th
.
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:
$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,'
',true);$three_lines=nsubstr('
',$wrapped_text,3);
print'
'.$three_lines;$four_lines=nsubstr('
',$wrapped_text,4);
print'
'.$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
*/?>
这篇博客介绍了一个PHP函数,该函数可以返回字符串中截至第N次出现的子串之前的文本。这个功能可以与`wordwrap()`结合使用,用于限制HTML文本的显示行数。示例代码展示了如何使用该函数,并给出了实际应用的例子。
2985

被折叠的 条评论
为什么被折叠?



