Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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 在前缀后面的字符串中获取11个字符_Php - Fatal编程技术网

Php 在前缀后面的字符串中获取11个字符

Php 在前缀后面的字符串中获取11个字符,php,Php,我想这样做 $name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky"; 输出应如下所示: “abcdefghijk” 共11个字符 请注意:XYZ是前缀方法substr()是您要查找的: 字符串子字符串(字符串$string,int$start[,int$length]) 返回由start和length参数指定的字符串部分 首先,在$name中找到$prefix的位置: strpos($name, $prefix) 然后需要添加前缀

我想这样做

$name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky";
输出应如下所示: “abcdefghijk”

共11个字符

请注意:XYZ是前缀方法
substr()
是您要查找的:

字符串子字符串(字符串$string,int$start[,int$length])

返回由start和length参数指定的字符串部分

首先,在$name中找到$prefix的位置:

strpos($name, $prefix)
然后需要添加前缀的长度以移动到正确的位置,并传递要在结果中获得的字符数(本例中为11):

试试这个

<?php

$name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky";

$out = substr(substr($name, strpos($name, 'XYZ'), 14), 3);

echo $out;

更新您尝试过的代码可能重复此代码片段可能是解决方案,但它确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。您是对的。我编辑了解决方案。
<?php

$name = "hello word XYZabcdefghijklmnopqrstuvwxyz bluesky";

$out = substr(substr($name, strpos($name, 'XYZ'), 14), 3);

echo $out;