0

Here's my test code:

$strings=array(
'Low Minimum',
'Low 10 Piece Minimum',
'10 piece minimum',
'20 piece minimum',
'low 6 piece minimum',
'100 piece minimum',
'This item is offered with a 1000 piece minimum',
'Place your order now. This item has a 75 piece minimum',
'Place your order now. This item has a low 75 piece minimum',
);

foreach($strings as $string)
  {
     echo preg_replace('/(low)? ?(\b[0-9]{0,3}\b) (piece minimum)/i',' Low Minumum',$string);
     echo '<br>';
  }

Here's the output:

Low Minimum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
This item is offered with a 1000 Low Minumum
Place your order now. This item has a Low Minumum
Place your order now. This item has a Low Minumum

Here is my desired output:

Low Minimum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
Low Minumum
This item is offered with a 1000 piece minimum
Place your order now. This item has a Low Minumum
Place your order now. This item has a Low Minumum

The one that messes me up is the 4 digit number. So, "piece minimum" should only match if some part of what's before it also matched. How do I write this?

TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • For one thing remove the comma in your last array entry `'Place your order now. This item has a low 75 piece minimum',` – Funk Forty Niner Oct 02 '13 at 18:15
  • @Fred-ii- having a comma after last specified array item is technically valid syntax in PHP. Though whether it is good coding practice is a different question altogether. – Mike Brant Oct 02 '13 at 18:17
  • @JosephSilber From what I've seen so many times before, the last entry shouldn't contain a comma, or are there exceptions to the rule? – Funk Forty Niner Oct 02 '13 at 18:17
  • 2
    Not only is it valid, but recommended. That way, when you subsequently add more lines, your commits won't affect that line. – Joseph Silber Oct 02 '13 at 18:18
  • @Fred-ii- All entries should have a trailing comma. – OIS Oct 02 '13 at 18:18
  • @JosephSilber Thanks for the info, I will definitely remember that. However, I don't why others with high rep points say that in the first place. If I had a nickel for everytime it's been said, I'd be on a Yacht somewhere. – Funk Forty Niner Oct 02 '13 at 18:20
  • @Fred-ii- - Would you let me join you? :P – Joseph Silber Oct 02 '13 at 18:21
  • @JosephSilber The more the merrier ;-) cheers – Funk Forty Niner Oct 02 '13 at 18:23
  • @Fred-ii- I think it's important to know that in some languages you are correct. In PHP, I do it because it is convenient when I am ready to add to the array. – TecBrat Oct 02 '13 at 18:23
  • Back in the day, JavaScript didn't support trailing commas. [All modern browsers now support trailing commas](http://stackoverflow.com/questions/7246618/trailing-commas-in-javascript#answer-7246662) (besides for..... wait for it....... IE8. Bet you didn't see that one coming). – Joseph Silber Oct 02 '13 at 18:25
  • @TecBrat You're right. I think we should probably get rid of all these comments and stick to the matter at hand, good idea? Or keep them as a learning curve? – Funk Forty Niner Oct 02 '13 at 18:26
  • I don't know how to do it, but I've heard of comments being moved to wiki. Maybe these could be. – TecBrat Oct 02 '13 at 18:31
  • @TecBrat May as well just leave well enough alone then ;-) – Funk Forty Niner Oct 02 '13 at 18:32

1 Answers1

1

For you desired output, you need to change a small mistake.

Instead of..

[0-9]{0,3}

It should be..

[0-9]{1,3}
hwnd
  • 69,796
  • 4
  • 95
  • 132