naf
naf

Reputation: 95

preg_match not working on last two charcters

i got his small code

 <?php  
    if (preg_match("/Remanufactured|REMANUFACTURED/",$product_info['products_description'] ) 
|| preg_match("/N2|N3|W1|W2|A1|A2|A3|R4/",$product_info['manufacturers_code'])) {
    echo "Refurbished";
   } else  {         
  echo "New Boxed"; 
 }
?>      

if $product_info['manufacturers_code'] = '2122586R4' or 'MC-MCB43112U0C2/04/1' the above code works.

I want the above code to only match where the last two characters are "/N2|N3|W1|W2|A1|A2|A3|R4/"

i have tried "/N2$|N3$|W1$|W2$|A1$|A2$|A3$|R4$/" and it doesnt work.

regards

irfan

Upvotes: 0

Views: 38

Answers (2)

Arun
Arun

Reputation: 3721

Replace

/N2|N3|W1|W2|A1|A2|A3|R4/

With

/^(.*)(N2|N3|W1|W2|A1|A2|A3|R4)$/

.* will match any string at the beginning and the specified two string at end

Upvotes: 1

Alex Anry
Alex Anry

Reputation: 101

Try this

preg_match('/(N2|N3|W1|W2|A1|A2|A3|R4)$/',$product_info['manufacturers_code'])

( ) - Groups a series of pattern elements to a single element.

| - Separates alternate possibilities.

$ - Matches the end of a line or string.

so "(a|b)" means "a or b". "(a|b)$" means "a or b" at the end of a line or string.

Upvotes: 1

Related Questions