我需要使用php从另一个字符串中提取子字符串。
$string = 'This is my string that conteined a Code:XYZ123';
$code = substr($text, -1, strpos($string,'Code:'));
echo $code ; // return 3
我需要整个代码,例如XYZ123
答案 0 :(得分:1)
您需要“代码:”的起始位置加上“代码:”的长度
$string = 'This is my string that conteined a Code:XYZ123';
$code = substr($string, strpos($string,'Code:') + 5);
echo $code; //XYZ123
答案 1 :(得分:1)
另一个选择是爆炸字符串my Code:
并回显数组的第二部分
$code = explode( 'Code:', $string );
echo $code[ 1 ]
答案 2 :(得分:0)
试试这段代码:
$string = 'This is my string that conteined a Code:XYZ123';
$code = substr($string ,strpos($string,'Code:'));
echo $code;
确保这对您有所帮助。