How do I correctly structure this regex statement?

I am trying to add extended warranties to my store for my products.  How do I structure this statement correctly?  I have attempted to escape the characters (+$ with no success.  

<tr class=hidden><td>Extended Warranty: </td><td><select name="p1" class=size><option selected>No Thanks</option>
<option>Add 1 Year Warranty (+$16.50)</option>
<option>Add 2 Year Warranty (+$37.50)</option>
<option>Add 3 Year Warranty (+$58.50)</option>
</td></tr>


Thanks for your help!!
preg_match('%<tr class=hidden><td>Extended Warranty: <\/td><td><select name="p1" class=size>
<option selected>(.*?)<\/option>
<option>Add (.*?) \(\+\$(.*?)\)<\/option>
<option>Add (.*?) \(\+\$(.*?)\)<\/option>
<option>Add (.*?) \(\+\$(.*?)\)<\/option>
<\/td><\/tr>%',$buf3,$matches7);
$warrantyno=$matches7[1];
$warranty1=$matches7[2];
$warranty1cost=$matches7[3];
$warranty2=$matches7[4];
$warranty2cost=$matches7[5];
$warranty3=$matches7[6];
$warranty3cost=$matches7[7];
echo $warrantyno."<br />";
echo $warranty1."<br />";
echo $warranty1cost."<br />";
echo $warranty2."<br />";
echo $warranty2cost."<br />";
echo $warranty3."<br />";
echo $warranty3cost."<br />";

Open in new window

rlb1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ropennerCommented:
preg_match_all will return more than one match, while preg_match will stop after 1.

A small sample is below to extract just the prices.

The first Array[0] is an array of

A)  the the full matching strings ...

and the second Array[1] is an array of

B) the first bracketed thing

then

C) the second bracketed thing .... etc. for more parenthesis in your search.

<?PHP 

$string = '<tr class=hidden><td>Extended Warranty: </td><td><select name="p1" class=size><option selected>No Thanks</option><option>Add 1 Year Warranty (+$16.50)</option><option>Add 2 Year Warranty (+$37.50)</option><option>Add 3 Year Warranty (+$58.50)</option>';
preg_match_all('/Add (.*?) \(\+\$([\d\.]*)\)/',$string, $matches);
print_r($matches);
/* OUTPUT:
Array
(
    [0] => Array
        (
            [0] => Add 1 Year Warranty (+$16.50)
            [1] => Add 2 Year Warranty (+$37.50)
            [2] => Add 3 Year Warranty (+$58.50)
        )

    [1] => Array
        (
            [0] => 1 Year Warranty
            [1] => 2 Year Warranty
            [2] => 3 Year Warranty
        )

    [2] => Array
        (
            [0] => 16.50
            [1] => 37.50
            [2] => 58.50
        )

)
*/
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
You need to double-up the backslashes on the regex escapes:
preg_match('%<tr class=hidden><td>Extended Warranty: <\\/td><td><select name="p1" class=size>
<option selected>(.*?)<\\/option>
<option>Add (.*?) \\(\\+\\$(.*?)\\)<\\/option>
<option>Add (.*?) \\(\\+\\$(.*?)\\)<\\/option>
<option>Add (.*?) \\(\\+\\$(.*?)\\)<\\/option>
<\\/td><\\/tr>%',$buf3,$matches7);

Open in new window

0
rlb1Author Commented:
Thanks for your help!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.