Why doesn't this print 12.99?
<?php $string = "I bought something for <strong>\$12.99</strong> last week!"; echo eregi_replace(".*trong>\$([^<]+).*", "\\1", $string); ?>
Could you provide me with a working regex?
TIA.
Try this:
".*trong>\\\$([^<]+).*"
$string = "I bought something for <strong>\$12.99</strong> last week!"; preg_match('!strong>\$([\d.]+)!i', $string, $match); echo $match[1];
Of course, whether or not a regex could be called "working" depends in large part on what it is going to be getting used for.....
Nice. Never would have gotten that one. I need ro reread my regex book.
Thanks much.
Kudose wrote:Nice. Never would have gotten that one. I need ro reread my regex book. Thanks much.
It's more a case of PHP and strings. In a double-quoted string, the back-slash escapes whatever follows it. In a regex, the $ has a special meaning, so you have to escape it. But in a double-quoted string a $ is the start of a variable unless you escape it. So we had to do two escapes in a row: one to escape the backslash and one to escape the $, thus the "\\$". Simple. 😉