1

我想从后面拆分一个字符串。喜欢:

$mystring = "this is my string";
$mysecondstring = "thisismystring";

我想从上面的字符串中拆分出最后 6 个字符,这里是“字符串”。我怎样才能在 PHP 中做到这一点?使用str_split,我可以从前面拆分,但我需要从最后。

提前致谢。

4

2 回答 2

2

利用substr()

编辑

$snippet = substr($mystring, 0, -6);
于 2013-03-27T00:12:09.517 回答
0

使用str_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"作为值的输出数组

于 2013-03-27T00:32:22.687 回答