Preg_match 表达式在字符串中查找代码

发布于 2024-11-07 17:52:26 字数 381 浏览 4 评论 0原文

示例字符串:

$string = "Buyer must enter coupon code 10OFF100 in shopping cart.";
$string = "Get $20 Off Auto Parts Order Over $150. Use Coupon code: GAPCJ20";

我需要提取代码(10FF100GAPCJ20)。

我试图创建一个 preg_match 来查找“优惠券代码”和“优惠券代码:”并立即提取后面的短语或单词。我自己运气不好,也没有找到 reg 表达式。

请一些人发布正确的代码。

感谢您的帮助。

Example strings:

$string = "Buyer must enter coupon code 10OFF100 in shopping cart.";
$string = "Get $20 Off Auto Parts Order Over $150. Use Coupon code: GAPCJ20";

I need to extract the codes (10FF100 and GAPCJ20).

I was trying to create a preg_match to find "coupon code" and "coupon code:" and extract the phrase or word immediately after. Not having much luck myself or finding the reg expression.

Can some post the proper code please.

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

稚气少女 2024-11-14 17:52:26

使用这个 php 代码:

$arr = array("Buyer must enter coupon code 10OFF100 in shopping cart.", "Get $20 Off Auto Parts Order Over $150. Use Coupon code: GAPCJ20");
foreach ($arr as $s) {
   preg_match('~coupon code\W+(\w+)~i', $s, $m);
   var_dump($m[1]);
}

输出

string(8) "10OFF100"
string(7) "GAPCJ20"

Use this php code:

$arr = array("Buyer must enter coupon code 10OFF100 in shopping cart.", "Get $20 Off Auto Parts Order Over $150. Use Coupon code: GAPCJ20");
foreach ($arr as $s) {
   preg_match('~coupon code\W+(\w+)~i', $s, $m);
   var_dump($m[1]);
}

OUTPUT

string(8) "10OFF100"
string(7) "GAPCJ20"
望笑 2024-11-14 17:52:26

请尝试以下操作:
<代码>/优惠券代码[:]? ([\w]*)/i

Try the following:
/coupon code[:]? ([\w]*)/i

oО清风挽发oО 2024-11-14 17:52:26

我的尝试:

<?php
if (preg_match('/coupon code:?\s+(.+)(\s+|$)/', $str, $matches)) {
  list(, $code) = $matches;
  echo $code;
}

My try:

<?php
if (preg_match('/coupon code:?\s+(.+)(\s+|$)/', $str, $matches)) {
  list(, $code) = $matches;
  echo $code;
}
笙痞 2024-11-14 17:52:26

对于您的第一个变体使用

Buyer must enter coupon code ([^ ]*) in shopping cart.

对于第二个使用

Get $20 Off Auto Parts Order Over $150. Use Coupon code: (.*)

For your first variant use

Buyer must enter coupon code ([^ ]*) in shopping cart.

For the second use

Get $20 Off Auto Parts Order Over $150. Use Coupon code: (.*)
最佳男配角 2024-11-14 17:52:26

我在下面做。

preg_match('/[Cc]oupon code:?(\w+)/',$string,$match);

I do below.

preg_match('/[Cc]oupon code:?(\w+)/',$string,$match);
软糯酥胸 2024-11-14 17:52:26

这会将可选的 : 与预计由字母和数字组成的“代码”相匹配。

/coupon code:? ([a-z0-9]+)/i

这行吗?

简短的回答:

那么如何让它更有效率呢?最好的办法是将正则表达式与行字典进行匹配,其中包含您想要支持的许多示例和格式。然后不断完善您的正则表达式,直到获得 98% 可靠的正则表达式。我说 98% 是因为您可能会意识到它永远不会完美,因为有很多:

  • 可能的拼写错误
  • 使用不同字符集的优惠券代码
  • 句子和单词顺序的变化,即
    • 免费优惠券:输入此代码 8HFnF5
    • 优惠券代码 (7859NhF)
    • 优惠券多多!只需将 646GH4 放入您的购物篮
    • 优惠码是797gGh
    • 您的免费礼品代码是 4543G5
    • 将代币“5479_G5t”添加到您的购物篮以获得免费 CD!
    • ...所有这些都会因上述正则表达式而失败

This will match optional : with a "code" which is expected to consist of alpha and numeric.

/coupon code:? ([a-z0-9]+)/i

Will this do?

Short answer: no.

So how to make it more efficient? Well the best thing to do would be to match a regex against a dictionary of lines that has many examples and formats you want to support. Then keep refining your regex until you get one that is 98% reliable. I say 98% because your probably come to the realization that it will never be perfect as there are so many:

  • Possible miss-spellings
  • Coupon codes using different characters sets
  • Variation of sentences and ordering of words i.e.
    • Free Coupon: Enter this code 8HFnF5
    • Code for coupon (7859NhF)
    • Coupons galore! Just put 646GH4 in your basket
    • Cupon code is 797gGh
    • Your code for free goodies is 4543G5
    • Add token "5479_G5t" to your basket for free CD!
    • ... all of these will fail with the above regex
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文