我需要选择字符串的一部分。我知道字符串的开始和结束点(使用strpos找到),但我不知道如何选择这部分字符串.... substr不会工作,因为字符串可以有不同的长度。我需要能够通过开始这个角色来截断字符串,并在这个角色结束。函数使用开始点和结束点截断为字符串
我敢肯定有办法做到这一点,但似乎无法发现它在手动
我需要选择字符串的一部分。我知道字符串的开始和结束点(使用strpos找到),但我不知道如何选择这部分字符串.... substr不会工作,因为字符串可以有不同的长度。我需要能够通过开始这个角色来截断字符串,并在这个角色结束。函数使用开始点和结束点截断为字符串
我敢肯定有办法做到这一点,但似乎无法发现它在手动
如前所述SUBSTR是完全没有问题。
我写这个答案只是为你提供的例子(因为它似乎你是一个有点糊涂)
$string = 'This string is too long and I want only a portion of it where I know the start and end position of the portion';
// now if you want to get everything between "too long" and "position" and you know the position for start and end which in this case are:
$start = 15; // 16th character
$end = 95; // 96th character
// from inside out first substr cuts the end of the string at requred character, the other substr cuts the beginning of the string
// resulting in portion beginning at 16th character and ending at 96th character.
$portion = substr(substr($string, 0, -(strlen($string) - $end)), $start);
var_dump($portion);
希望帮助你了:)
http://php.net/manual/en/function.substr.php
SUBSTR这是否完美,它需要开始位置和长度。 http://php.net/manual/en/function.strlen.php
您可以找到字符串的长度与本 -
与(起始位置结束位置)计算长度(string $ string,int $ start [,int $ length])
返回由start和length指定的部分字符串米。
http://www.php.net/manual/en/function.substr.php
SUBSTR - 一个字符串 串SUBSTR的收益部分:
使用substr()
与你从strpos()
得到的变量;
$string = 'My sub string in a string';
$substring = 'sub string';
echo substr($string, strpos($string, $substring), strlen($substring));
// echoes "sub string";
那种愚蠢的例子,但你明白了吧:)
OF当然!对不起,我以为我有一个金发的时刻。这是一个艰难的一天!谢谢大家。 –
如果你知道的开始和结束,为什么'substr'不能工作? – Josh