php php拼接字符串函数_PHP: 字符串 函数 - Manual

这篇博客介绍了一个PHP函数,该函数可以返回字符串中截至第N次出现的子串之前的文本。这个功能可以与`wordwrap()`结合使用,用于限制HTML文本的显示行数。示例代码展示了如何使用该函数,并给出了实际应用的例子。
摘要由CSDN通过智能技术生成

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

*/?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值