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

在php中将字符串提取为长字符串中的字符串

在php中将字符串提取为长字符串中的字符串,php,string,Php,String,我在互联网上搜索了一下,找到了解决这个问题的方法,但它的顺序正好相反 原始字符串 S0597 KIPATIMU SECONDARY DIV-I = 0 DIV-II = 3 要提取的字符串 S0597 KIPATIMU SECONDARY extraction should stop when "DIV-" is reached. 我的代码输出 DIV-I = 0 DIV-II = 3 这是我的代码。 if(($pos = strpos($string, 'DIV-')) !==

我在互联网上搜索了一下,找到了解决这个问题的方法,但它的顺序正好相反

原始字符串

S0597 KIPATIMU SECONDARY DIV-I = 0  DIV-II = 3  
要提取的字符串

S0597 KIPATIMU SECONDARY

extraction should stop when "DIV-" is reached.
我的代码输出

DIV-I = 0  DIV-II = 3
这是我的代码。

if(($pos = strpos($string, 'DIV-')) !== false)
{
  $school_name = substr($string, $pos);
}
else
{
   $school_name = get_last_word($string);
}
echo $school_name;
我怎样才能做到这一点

$school_name = substr($string, $pos);
这意味着您正在分配子字符串为
$string
$school\u name
,从
DIV-
到达
$string
末尾的位置开始

您需要从
0
开始,直到到达
DIV-
的位置

$school_name = substr($string, 0, $pos);
这意味着您正在分配子字符串为
$string
$school\u name
,从
DIV-
到达
$string
末尾的位置开始

您需要从
0
开始,直到到达
DIV-
的位置

$school_name = substr($string, 0, $pos);

您将获得$school_name中的提取字符串,然后将其替换为完整字符串中的空白字符串,您将获得输出

if(($pos = strpos($string, 'DIV-')) !== false)
{
  $school_name = substr($string, $pos);
  $school_name = str_replace($school_name, '', $string);
}
// output - S0597 KIPATIMU SECONDARY 

您将获得$school_name中的提取字符串,然后将其替换为完整字符串中的空白字符串,您将获得输出

if(($pos = strpos($string, 'DIV-')) !== false)
{
  $school_name = substr($string, $pos);
  $school_name = str_replace($school_name, '', $string);
}
// output - S0597 KIPATIMU SECONDARY 

$str_value='S0597 KIPATIMU SECONDARY DIV-I=0 DIV-II=3'//存储字符串值

在PHP中,有许多函数可用于这些功能,您已经使用了多个函数来实现这些功能

stristr($str_值,“DIV-”,true)//这些函数返回o/p:S0597 KIPATIMU SECONDARY


如果将最后一个参数从'true'更改为'false',它将返回DIV-I=0 DIV-II=3$str_value='S0597 KIPATIMU SECONDARY DIV-I=0 DIV-II=3'//存储字符串值

在PHP中,有许多函数可用于这些功能,您已经使用了多个函数来实现这些功能

stristr($str_值,“DIV-”,true)//这些函数返回o/p:S0597 KIPATIMU SECONDARY

如果将最后一个参数从“true”更改为“false”,它将返回DIV-I=0 DIV-II=3