Reputation: 61
I have a problem! I wanna detect any numbers in HTML content without numbers in tag attributes, I wanna change this numbers to other character then only numbers not in HTML TAG ATTRIBUTES that match with this REGEX.
Example:
Hi 3456; <a href="?id=4456">your code: 345</a>
Matched 3456, 345 Not Matched 4456
Thanks from all
Upvotes: 1
Views: 177
Reputation: 11110
Here's a quick dirty way that will work for simple samples and for valid html, and probably will cause problems with invalid html:
<?php
$html='Hi 3456; <a href="?id=4456">your code: 345</a> another 234';
$html = preg_replace('|(>[^<\d]*)(\d+)([^<\d]*</)|', '$1{NUM_WAS_HERE}$3', $html);//match between tags
$html = preg_replace('|^([^<\d]*)(\d+)([^<\d]*<)|', '$1{NUM_WAS_HERE}$3', $html);//beginning of the string
$html = preg_replace('|(>[^<\d]*)(\d+)([^<\d]*)$|', '$1{NUM_WAS_HERE}$3', $html);//end of the string
echo $html, "\n";//outputs: Hi {NUM_WAS_HERE}; <a href="?id=4456">your code: {NUM_WAS_HERE}</a> another {NUM_WAS_HERE}
As @Reinis recommended, using an html parser is the good secure way to achieve this.
Upvotes: 0
Reputation: 8158
You should best use a parser like PHP Simple HTML DOM Parser. The reasons are outlined in this blog post.
Upvotes: 1