I want to postfix every link in a HTML file with a Google Analytics tracking code. The entire HTML is contained in the $content variabile. Is it possible to add this tracking code to all links, except mailto?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, you can’t do it – at least not reliably so. HTML is very contextual, which means you need a real parser to pull this off. Regular expressions may cover many cases, but you’ll end up with both false positives (your regex matching on something that is not really a link) and false negatives (real links being missed). See the link in my Pony comment for a more thorough… uhm… “explanation”.
If you really have to go through the final HTML and post-process it, your best bet is to find a proper HTML parser (in a pinch,
DOMDocumentmight do: IIRC, it can parse both XML and HTML), walk through the DOM tree and replace links as appropriate, then render the tree back into a string.Ideally though, you have an HTML-aware template system in place (e.g. XSLT), in which case you can probably intercept the DOM tree earlier in the process, which means you can skip the additional parsing and rendering steps and go right to the DOM tree.