I’m attempting to find a value X, which matches the pattern:
<span class="line-item-quantity-raw">X</span>
Once I’ve got the value, I want to trim everything else around it. While I know I could do this with preg_match just to get the value and set the variable, I’m quite curious as to why I can’t get my original method with preg_match to function.
$footer = '<div class="line-item-summary">
<div class="line-item-quantity">
<span class="line-item-quantity-raw">1</span>
<span class="line-item-quantity-label">item</span>
</div>
<div class="line-item-total">
<span class="line-item-total-label">Total:</span>
<span class="line-item-total-raw">$1,500.00</span>
</div>
</div>';
$pattern = '/.*<span class="line-item-quantity-raw">(\d+)<\/span>.*/';
$replace = '($1) -';
$footer = preg_replace($pattern, $replace, $footer);
Unfortunately this only seems to strip out the span tags as specified in the $pattern however the extra markup on the edges of the $pattern such as .* is still being kept.
Annoyingly running my code in a test page such as http://www.solmetra.com/scripts/regex/index.php seems to work, just not my code above in php.
This is because
.is not matching by default line breaks. To make.span multiple lines, you need to add the modifiers:http://php.net/manual/en/reference.pcre.pattern.modifiers.php