在php中,如何在字符串中的特定单词后获得下一个12个字母长的单词

在php中,如何在字符串中的特定单词后获得下一个12个字母长的单词,php,Php,我下面有一根绳子 $string='收件人:发票号开票日期704319003027 15-06-2019 PAPIYA DUTTA销售订单号销售订单日期PAIKPARA,Muraai路地区:NALHATI BIRBHUM-731220' 我要在“发票号”后面加上12个字符长的“704319003027” $string = 'Invoice Number: Invoice Name: 704319003027 Rahul Sinha Account Information: Some tex

我下面有一根绳子 $string='收件人:发票号开票日期704319003027 15-06-2019 PAPIYA DUTTA销售订单号销售订单日期PAIKPARA,Muraai路地区:NALHATI BIRBHUM-731220'

我要在“发票号”后面加上12个字符长的“704319003027”

$string = 'Invoice Number: Invoice Name: 704319003027 Rahul Sinha 
 Account Information: Some text here';

 $invoice_no = substr($string, strpos($string, 'Invoice Number:') + strlen($matches[0]), 12);
echo $invoice_no;
选择1 选择2 您可以使用preg_match提取发票名称以下的编号:

preg_match('/Invoice Name: ([\d]+) /', $string, $matches);
echo $matches[1];

使用正则表达式匹配字符串中所需的文本,如下所示:

$string = 'Invoice Number: Invoice Name: 704319003027 Rahul Sinha Account Information: Some text here';
preg_match('@Invoice Name: (\d{12})@',$string,$matches);
if( !empty( $matches ) )echo $matches[1];

您可以使用preg的lookback来获取发票编号后有12个字符的第一个编号


在“发票号”或“发票名”之后?在发票号之后。但是,在您的示例中,“发票号”之后没有数字。“发票号:发票名:704319003027 Rahul Sinha帐户信息:此处的一些文本”好的解决方案:我可以在此处使用发票号,而不是发票名,@发票编号而不是@Invoice Name您使用的字符串不正确,该字符串类似于$string='发票编号:发票名称:704319003027 Rahul Sinha帐户信息:此处有一些文本';我们只需要使用“发票编号”来获取编号
preg_match('/Invoice Name: ([\d]+) /', $string, $matches);
echo $matches[1];
$string = 'Invoice Number: Invoice Name: 704319003027 Rahul Sinha Account Information: Some text here';
preg_match('@Invoice Name: (\d{12})@',$string,$matches);
if( !empty( $matches ) )echo $matches[1];
if(preg_match("/(?<=Invoice Number).*([0-9]{12})/",$string,$matches)){
    echo var_dump($matches[1]);
}