我正在寻找一种方法从字符串变量中拉出前100个字符以放入另一个变量进行打印.
有没有可以轻松完成此功能的功能?
例如:
$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";
$string2 = 100charfunction($string1);
print $string2
Run Code Online (Sandbox Code Playgroud)
要得到:
I am looking for a way to pull the first 100 characters from a string vari
Run Code Online (Sandbox Code Playgroud)
Pat*_*ins 178
$small = substr($big, 0, 100);
Run Code Online (Sandbox Code Playgroud)
对于String Manipulation,这里有一个包含许多功能的页面,可以帮助您将来的工作.
Ste*_*aug 34
我想你可以使用substr:
$string2 = substr($string1, 0, 100);
Run Code Online (Sandbox Code Playgroud)
或mb_substr用于多字节字符串:
$string2 = mb_substr($string1, 0, 100);
Run Code Online (Sandbox Code Playgroud)
您可以创建一个使用此函数的函数,并附加例如'...'表示它已缩短.(我想在发布这个帖子时已经有一百个类似的回复...)
mar*_*kus 20
$x = '1234567'; echo substr ($x, 0, 3); // outputs 123 echo substr ($x, 1, 1); // outputs 2 echo substr ($x, -2); // outputs 67 echo substr ($x, 1); // outputs 234567 echo substr ($x, -2, 1); // outputs 6
Coz*_*Coz 19
作为一个迟到但有用的答案,PHP具有专门用于此目的的功能.
$string = mb_strimwidth($string, 0, 100);
$string = mb_strimwidth($string, 0, 97, '...'); //optional characters for end
Run Code Online (Sandbox Code Playgroud)
小智 18
试试这个功能
function summary($str, $limit=100, $strip = false) {
$str = ($strip == true)?strip_tags($str):$str;
if (strlen ($str) > $limit) {
$str = substr ($str, 0, $limit - 3);
return (substr ($str, 0, strrpos ($str, ' ')).'...');
}
return trim($str);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157494 次 |
| 最近记录: |