1

Okay, I have a php loop that generates a string like this:

$string = "1st fee - $500.00<BR>
           2nd fee - $500.00<BR>
           3rd fee - $750.00<BR>"

I'd like to remove the dollar amount but I'm not sure it's even possible. I've been trying to use preg_replace to remove everything between $ and < including the $ but not the < to get something like this:

   $string = "1st fee - <BR>
               2nd fee - <BR>
               3rd fee - <BR>"

Unfortunately for me, I have to leave the - because sometimes there will be multiple instances of the - on a single line like so:

$string = "1st fee - Brian - $500.00<BR>
           2nd fee - John - $500.00<BR>
           3rd fee - Bob - $750.00<BR>"

Any help would be greatly appreciated, even if it's just telling me give up, not possible. Thanks in advance!

Jeff Prachyl
  • 277
  • 1
  • 3
  • 11
  • What regex did you try? What was the result? * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Feb 06 '13 at 22:21
  • if `
    ` is supposed to be a break tag, it's deprecated. Use `
    `
    – Sterling Archer Feb 06 '13 at 22:22
  • @PRPGFerret What the heck are you talking about? – Niet the Dark Absol Feb 06 '13 at 22:22
  • preg_replace('/\$[^<]*/', '', $text) –  Feb 06 '13 at 22:23
  • 1
    why not edit the loop that generates the sting in the first place? –  Feb 06 '13 at 22:25
  • @Kolink the
    in the string, if that's a break tag it's a deprecated tag.
    – Sterling Archer Feb 06 '13 at 22:30
  • 1
    `
    ` is a tag that has no closing tag, just like ``, ``, ``, `
    ` and so on. The XHTML self-closing syntax is frequently used for clarity (to avoid confusion) but is in no way required.
    – Niet the Dark Absol Feb 06 '13 at 22:33

3 Answers3

1

So basically you just want to match everyhing from - $ until the first left arrow in the <br>. The first part of the pattern - \\$ (need to escape the $ since that is a regex keyword) will match the beginning part of what you want removed, and then [^<]* will take everything until it finds a left arrow.

$string = "1st fee - Brian - $500.00<BR>
           2nd fee - John - $500.00<BR>
           3rd fee - Bob - $750.00<BR>";

$replaced = preg_replace("# - \\$[^<]*#", "", $string);

Output:

string '1st fee - Brian<BR>
        2nd fee - John<BR>
        3rd fee - Bob<BR>'
Supericy
  • 5,866
  • 1
  • 21
  • 25
0

You can use the following example to remove the amounts and the last -

<?php

$string = "1st fee - $500.00<BR>
           2nd fee - $500.00<BR>
           3rd fee - Bob - $750.00<BR>";

$pattern = '/\- \$[0-9]+\.[0-9]+\<BR>$/m';

echo preg_replace($pattern, '', $string);

Note that because of the /m modifier the pattern matches multiple lines, where the $ ( at the end of the pattern) matches the end of a line. This way it is possible to match every line from right to left and remove the amount and only the last - in it.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • preg_replace replace all matching pattern, you don't need /m modifier – Code-Source Feb 06 '13 at 22:52
  • Have you read my post? Of course I need it. Thats the final trick in my example. Note that, currently my example is the only that matches correctly: **- $000.00
    \n**
    – hek2mgl Feb 06 '13 at 22:55
  • @Code-Source If you do not understand a post. Please do not downvote it – hek2mgl Feb 06 '13 at 23:01
0

I think you should use the full format. You want to replace an amount so you should use a regexp that correspond to an amount.

$reg = '/ - \$[0-9]+\.[0-9]{2}/';
$withoutAmount = preg_replace($reg, '', $string);

You can improve the regexp with other currencies:

$reg = '/ - (\$|£|€)[0-9]+\.[0-9]{2}/':

To test it: http://phpfiddle.org/main/code/auw-gb4

Code-Source
  • 2,215
  • 19
  • 13