php 提取前一百个字,如何在PHP中提取字符串的前100個字符?
I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.
我正在尋找一種方法,從字符串變量中提取前100個字符,並將其放入另一個變量中進行打印。
Is there a function that can do this easily?
有一個函數(shù)可以很容易地做到這一點嗎?
For example:
例如:
$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
To get:
得到:
I am looking for a way to pull the first 100 characters from a string vari
6 個解決方案
#1
151
$small = substr($big, 0, 100);
For String Manipulation here is a page with a lot of function that might help you in your future work.
對於字符串操作,這里有一個具有很多功能的頁面,這些功能可能對您以后的工作有所幫助。
#2
28
You could use substr, I guess:
你可以用substr,我猜
$string2 = substr($string1, 0, 100);
or mb_substr for multi-byte strings:
或多字節(jié)串的mb_substr:
$string2 = mb_substr($string1, 0, 100);
You could create a function wich uses this function and appends for instance '...' to indicate that it was shortened. (I guess there's allready a hundred similar replies when this is posted...)
你可以創(chuàng)建一個函數(shù),它使用這個函數(shù)並附加例如'…表示它被縮短了。(我想當(dāng)這篇文章發(fā)布的時候,已經(jīng)有上百個類似的回復(fù)了…)
#3
19
$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
#4
18
try this function
試試這個功能
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);
}
#5
15
A late but useful answer, PHP has a function specifically for this purpose.
PHP有一個專門用於此目的的函數(shù),這是一個很晚但很有用的答案。
mb_strimwidth
$string = mb_strimwidth($string, 0, 100);
$string = mb_strimwidth($string, 0, 97, '...'); //optional characters for end
#6
2
Without php internal functions:
沒有php內(nèi)部函數(shù):
function charFunction($myStr, $limit=100) {
$result = "";
for ($i=0; $i
$result .= $myStr[$i];
}
return $result;
}
$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";
echo charFunction($string1);
總結(jié)
以上是生活随笔為你收集整理的php 提取前一百个字,如何在PHP中提取字符串的前100個字符?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 明天全国高考 祝所有考生金榜题名!高考“
- 下一篇: 谷歌被无视了 经销商违反规则提前开卖Pi