1

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

2 Answers 2

1

You should best use a parser like PHP Simple HTML DOM Parser. The reasons are outlined in this blog post.

Sign up to request clarification or add additional context in comments.

1 Comment

Suggested third party alternatives to SimpleHtmlDom that actually use DOM instead of String Parsing: phpQuery, Zend_Dom, QueryPath and FluentDom.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.