Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.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_String_Split - Fatal编程技术网

在PHP中,基于从结束到结束的长度拆分字符串

在PHP中,基于从结束到结束的长度拆分字符串,php,string,split,Php,String,Split,我想从后面分开一根绳子。比如: $mystring = "this is my string"; $mysecondstring = "thisismystring"; 我想从上面的字符串中拆分最后6个字符,这里是“string”。如何在PHP中实现这一点? 使用stru-split,我可以从正面拆分,但我需要从末端拆分 提前感谢。使用 编辑 $snippet = substr($mystring, 0, -6); 使用 编辑 $snippet = substr($mystring, 0,

我想从后面分开一根绳子。比如:

$mystring = "this is my string";
$mysecondstring = "thisismystring";
我想从上面的字符串中拆分最后6个字符,这里是“string”。如何在PHP中实现这一点? 使用stru-split,我可以从正面拆分,但我需要从末端拆分

提前感谢。

使用

编辑

$snippet = substr($mystring, 0, -6);
使用

编辑

$snippet = substr($mystring, 0, -6);
使用
str\u split()
将字符串转换为数组

但是可以以数组形式获得与所需相同的结果输出

// The first parameter would the string you want to split.
// then the second parameter would be in what length you want the string to be splitted.

str_split(parameter1,parameter2)
例如

$mystring = " this is my string ";

// we put length of string to be splitted by 6 because its the string length of "string"
$myString = str_split($myString,6);
它将产生以下结果:

Output:
Array
(
    [0] => " this " 
    [1] => "is my "
    [2] => "string"

// then you can 
echo $string[2] 
//for the value of "string" being splitted.
)

注意:如果您使用
str_split()
来获取所需的值,则必须为其提供一个良好的除法长度。这就是为什么我在
$mystring
的值中添加额外的空格,以仅生成
的输出数组“string”
,因为使用
stru split()
将字符串转换为数组

但是可以以数组形式获得与所需相同的结果输出

// The first parameter would the string you want to split.
// then the second parameter would be in what length you want the string to be splitted.

str_split(parameter1,parameter2)
例如

$mystring = " this is my string ";

// we put length of string to be splitted by 6 because its the string length of "string"
$myString = str_split($myString,6);
它将产生以下结果:

Output:
Array
(
    [0] => " this " 
    [1] => "is my "
    [2] => "string"

// then you can 
echo $string[2] 
//for the value of "string" being splitted.
)


注意:如果您使用
str_split()
来获取所需的值,则必须为其提供一个良好的除法长度。这就是为什么我在
$mystring
的值中添加了额外的空格,以便只生成一个
“string”
作为值的输出数组

我想删除“string”,不想显示最后6个字符。更新以反映您的澄清我想删除“string”,不想显示最后6个字符。更新以反映您的澄清