I have an xhtml string, and it contains:
<a id="8" />
The number is random each time. It does not have an href attribute, so maybe removing all <a>
without href is an option? I want to remove those parts with PHP, what is my best approach?
I would like to use tidy, but I don't think it has an option to remove those tags.
If that is not possible, I believe preg_replace()
with regex is my second best approach. However, I do not know the regex string to remove those parts.
I was able to fix it, by finding out why this piece of code was added to the XHTML string in the first place. I removed it, thereby solving the problem.
Try this: <a\s+id="[^"]*"[^\/]\/>
$re = "/<a\\s+id=\"[^\"]*\"[^\\/]\\/>/mi";
$str = "<a id=\"8\" />
jdf
sadfa
<a id=\"df\" />
<a name=\"df\" />";
$subst = "";
$result = preg_replace($re, $subst, $str);
And only remove <a id="8" />
then use <a\s+id="8"[^\/]\/>