Preg_match assumption for finding code in a string

Examples of lines:

$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 ( 10FF100and GAPCJ20).

I tried to create preg_matchto find the "coupon code" and "coupon code:" and immediately extract the phrase or word. I did not manage to find the expression reg.

Can someone send the correct code please.

Thanks for the help.

+3
source share
6 answers

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"
+1
source

This will correspond to an optional :with a “code”, which is expected to consist of letters and numbers.

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

Will it do it?

The short answer is no .

, ? , , , . , , 98% . 98%, , , , , :

  • , .
    • 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!
    • ...
0

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

0

:

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

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

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

.

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

All Articles