Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 函数使用起点和终点截断为字符串_Php - Fatal编程技术网

Php 函数使用起点和终点截断为字符串

Php 函数使用起点和终点截断为字符串,php,Php,我需要选择字符串的一部分。我知道字符串的起点和终点(使用strpos找到),但我不知道如何选择字符串的这部分…substr不起作用,因为字符串的长度可能不同。我需要能够截断字符串,说从这个字符开始,到这个字符结束 我确信有办法做到这一点,但在手册中似乎找不到 substr做得很好,它需要起始位置和长度 使用(结束位置-开始位置)计算长度 substr做得很好,它需要起始位置和长度 使用(结束位置-开始位置)计算长度。您可以使用以下方法找到字符串的长度: 您可以通过以下方法找到字符串的长度: s

我需要选择字符串的一部分。我知道字符串的起点和终点(使用strpos找到),但我不知道如何选择字符串的这部分…substr不起作用,因为字符串的长度可能不同。我需要能够截断字符串,说从这个字符开始,到这个字符结束

我确信有办法做到这一点,但在手册中似乎找不到

substr做得很好,它需要起始位置和长度

使用(结束位置-开始位置)计算长度

substr做得很好,它需要起始位置和长度


使用(结束位置-开始位置)计算长度。

您可以使用以下方法找到字符串的长度:

您可以通过以下方法找到字符串的长度:

substr-返回字符串的一部分 字符串子字符串(字符串$string,int$start[,int$length])

返回由起始参数和长度参数指定的字符串部分。

substr-返回字符串的一部分 字符串子字符串(字符串$string,int$start[,int$length])

返回由起始参数和长度参数指定的字符串部分。

substr()
与从
strpos()获取的变量一起使用。

这是一个愚蠢的例子,但你明白了一点:)

使用
substr()
和你从
strpos()得到的变量


这是一个愚蠢的例子,但你明白了一点:)

正如前面提到的,substr是非常好的

我写这个答案只是为了给你举个例子(因为你似乎有点困惑)


希望这能帮助你:)

如前所述,substr非常好

我写这个答案只是为了给你举个例子(因为你似乎有点困惑)


希望这能帮助你:)

如果你知道开始和结束,为什么
substr
不能工作?如果你知道开始和结束,为什么
substr
不能工作?当然!对不起,我想我在那里有一个金发的时刻。这是艰难的一天!谢谢大家,当然!对不起,我想我在那里有一个金发的时刻。这是艰难的一天!谢谢大家。
$string = 'My sub string in a string';
$substring = 'sub string';
echo substr( $string, strpos( $string, $substring ), strlen( $substring ) );
// echoes "sub string";
$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);