I have a form that I am validating with JS on the front-end and PHP on the server side. What I need is a way to reliably count the number of links in an HTML string. The best way that I could think of was to count the closing tags. However simply searching for this tag will not work because the user could circumvent the validation by adding spaces like so: </a >
.
I am fairly new to regex and this is the pattern that I have been able to come up with so far:
<[ \n\t]*\/[ \n\t]*a[ \n\t]*>
In Javascript:
function link_count(s){
return s.match(/<[ \n\t]*\/[ \n\t]*a[ \n\t]*>/g, s).length;
}
In PHP:
function count_links($str){
return preg_match_all('<[ \n\t]*/[ \n\t]*a[ \n\t]*>', $str, $matches);
}
Is this the best approach? Will it affect the performance of my form (the html string could be very long)? I am looking for the most efficient and reliable solution.
Thanks in advance.