在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个字符。更新以反映您的澄清