在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