为了账号安全,请及时绑定邮箱和手机立即绑定

如何在php中获取具有匹配文本的所有行

如何在php中获取具有匹配文本的所有行

PHP
阿晨1998 2022-08-19 16:46:32
我有一个字符串供练习。$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";我想使用preg_match获取索赔代码,但我失败了。这是我的代码。 if(preg_match_all("/claim code:.*\s/", $message, $array)){             print_r($array);        }输出是这个Array ( [0] => Array ( [0] => claim code: ) )但我希望它显示索赔代码,任何人都可以帮忙吗?
查看完整描述

2 回答

?
MM们

TA贡献1886条经验 获得超2个赞

我会使用这个模式:(?<=\bclaim code: )\S+


$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";

preg_match_all("/(?<=\bclaim code: )\S+/", $message, $matches);

print_r($matches[0][0]);

这打印:


NE7N-TUD6NV-GUAB

您当前方法的主要问题是正则表达式模式本身。 匹配零个或多个空格字符,您应该像我上面使用的那样使用。\s*\S*


查看完整回答
反对 回复 2022-08-19
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

你可以使用积极的观察来实现这个目标。我建议使用,除非你有多个索赔代码。因为内存消耗比 .对于您的解决方案,请查看以下代码:preg_matchpreg_match_all()preg_match()


$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";


preg_match('/(?<=\bclaim\scode\b:\s)\S+/', $message, $matches);

echo $matches[0]; // NE7N-TUD6NV-GUAB

正面观察:


确保给定的模式匹配,在表达式中的当前位置结束。图案必须具有固定的宽度。不消耗任何字符。


语法:


(?<=...)


查看完整回答
反对 回复 2022-08-19
  • 2 回答
  • 0 关注
  • 251 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号