Match Numbers in HTML content (not in tag attributes) with REGEX in PHP
I have a problem! I wanna detect any numbers in HTML content without numbers in tag attributes, I wanna开发者_如何学Go 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
You should best use a parser like PHP Simple HTML DOM Parser. The reasons are outlined in this blog post.
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.
精彩评论