preg_replace question: remove text in between all occurrences of 2 strings

Hi, I have a list of order numbers (always 6 digits) followed by a dollar-amount and I’d like the preg_match to locate all the order numbers and the dollar amounts in a long string cluttered with html.

Example:

<?php $string = 'here is my order879475 for about $7.58and I want order this thing 327287 for $20.00 but only if
767654 is worth $123.88 if that is okay. Then order#342435 is good for $0.05.'; ?>

Desired result:
879475 7.58
327287 20.00
767654 123.88
342435 .05

Whoa… I just realized this is kinda complicated. I sure thank you for taking a look at this!!

This regular expression should work for your task:
[php]<?php

$string = ‘here is my order879475 for about $7.58and I want order this thing 327287 for $20.00 but only if
767654 is worth $123.88 if that is okay. Then order#342435 is good for $0.05.’;

if(preg_match_all(’~([\d]{6})[^$]$([\d].?[\d]+)~i’,$string,$matches,PREG_SET_ORDER)){
foreach($matches as $arr){
echo $arr[1].’ - $’.$arr[2].’
’;
}
}

?>[/php]

(find all numbers of exactly 6 digits, followed by anything that is not $ sign, followed by $ sign and decimal number)

I am getting a “parse error” and I’ve tried swapping out everything that would make a parse error, but it won’t work?

It works! (nevermind parse error – my mistype). Thank you!!! Merry Christmas!!! :slight_smile:

No problem! Merry Christmas :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service