2

A client has ~7,000 products with a "Your Price: $..." in the description, the price is typed in (there is no existing wildcard).

Here is an example of a description:

<table cellpadding="5" border="0" width="100%"><tbody><tr><td><strong>Part #: </strong></td><td>FIV000-2100</td></tr><tr><td><strong>Retail Price: </strong></td><td>$26.39</td></tr><tr><td class="price"><strong>Your Price: </strong></td><td class="price">$23.75</td></tr><tr><td align="center" colspan="2"/></tr></tbody></table>

Is there a regular expression to use to just remove the Your Price row? What is we wanted to remove the Retail Price row as well?

Any help would be greatly appreciated!

AVProgrammer
  • 1,344
  • 2
  • 20
  • 40

3 Answers3

3

You can do something like this (provided $str is the html string):

$pattern = "/<tr><td class=\"price\"><strong>Your Price: <\/strong><\/td><td class=\"price\">\\$[0-9.]+<\/td><\/tr>/";
$str = preg_replace($pattern, "", $str);

The empty string will replace it with nothing, thus removing it.

EDIT:

Escaped some stuff to make it work. I also urge you to use a HTML parser. Let's call this the quick and dirty method.

Gustav Larsson
  • 7,679
  • 2
  • 29
  • 50
  • +1 This one will work quite effectively if all the records are formatted the same, but you do need to escape the `$` and all the `/` chars in the pattern. (or better yet, choose a different regex delimiter). You should probably add a comma to the char class (in case of `$1,234.56`) – ridgerunner Apr 04 '11 at 19:29
  • You have an unescaped forward slash on your first `` there. – Justin Morgan Apr 04 '11 at 20:42
  • @Justin E. Morgan Thanks! Fixed. – Gustav Larsson Apr 04 '11 at 20:49
3

I strongly recommend you use an HTML parser for this. That being said, the following will most likely do what you want:

/Your Price:.*?\$(\d+(?:,\d+)?(?:\.\d+)?)/

I don't know MySQL regex syntax, so you might want to double-check that.

Community
  • 1
  • 1
Justin Morgan
  • 28,775
  • 12
  • 76
  • 104
1

You really don't want to parse or modify HTML with regular expressions... Just use something made for it, for example phpQuery: http://code.google.com/p/phpquery/

Capsule
  • 6,072
  • 1
  • 18
  • 27