从字符串中提取子字符串使用php

问题描述:

我需要从另一个字符串中使用php提取子字符串。从字符串中提取子字符串使用php

$string = 'This is my string that conteined a Code:XYZ123'; 
$code = substr($text, -1, strpos($string,'Code:')); 
echo $code ; // return 3 

我需要例如整个代码XYZ123

的第二部分试试这个代码:

$string = 'This is my string that conteined a Code:XYZ123'; 
$code = substr($string ,strpos($string,'Code:')); 
echo $code; 

确保这对您有帮助。

+0

未定义的变量:代码 –

+0

输出到这是'代码:XYZ123' – Blinkydamo

+0

PLZ再试一次我已经更新我的代码 –

您需要的起始位置“代码:”加长度“代码:”

$string = 'This is my string that conteined a Code:XYZ123'; 
$code = substr($string, strpos($string,'Code:') + 5); 
echo $code; //XYZ123 

另一种选择是爆炸串我Code:和回声阵列

$code = explode('Code:', $string); 
echo $code[ 1 ]