• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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

0
rlb1
Asked:
rlb1
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now