I wrote a regex in Rad Soft to pull out a div tag from some html. It tested well in the editor and php regex tester. Now I am trying to get it to implement with php on my current project but just doesn’t work. Usually its some simple solution that is failing to register since I have been staring at the problem too long.
here is the test sample being used
$temp = '<div>blah blah blah.
You ordered these items:
<div id="product_list">
- 1 Large - Target
<span class="pricedisplay">R$ 1,00</span>
- 1 Large - Black
<span class="pricedisplay">R$ 1,00</span>
- 1 Large - Leather
<span class="pricedisplay">R$ 1,00</span>
- 1 Large - Preto
<span class="pricedisplay">R$ 1,00</span>
</div><span class="total-shipping"></span><span class="total-price">Total: <span class="pricedisplay">R$ 1,00</span>
</span></div>';
and for the regex:
<div\sid="product_list">([\s\w\d<=>/\\\$:",\.-]?)*[^(</div>)]</div>
which I tried using here:
$outputHTML = preg_replace_callback(preg_quote('<div\sid="product_list">([\s\w\d<=>/\\\$:",\.-]?)*[^(</div>)]</div>'), 'findProducts', $temp);
I have tried with/without the preg_quote. And if you know of any software similar to Rad that better simulates php’s regex, it would help alot. Thanks in advance!
Regardless of whether this regex happened to work with some tester there are many mistakes in it.
First :
Equals too :
You need not unescape characters inside character class. Additionally you use preg_quote which escapes :
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -Secondly :
Means :
While what you probably wanted to write is :
Lastly :
([\s\w\d<=>/\\\$:",\.-]?)*equals :[\s\w\d<=>/$:",.\\-]*Oh and yeah, what you are doing is potentially dangerous. You shouldn’t try to extract/parse html/xml/xhtml with regex when you have ready to use tools in your language.