1

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.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
user1557314
  • 169
  • 3
  • 14
  • 1
    Related: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – akxlr Apr 10 '15 at 08:17
  • If the id is set with a function that just outputs a random number, you can just get that random number in a variable, then use it in the link but keep the variable and use it to identify which tag to remove. – Loko Apr 10 '15 at 08:21
  • Are you using any XML parsing library/functions? – Salman A Apr 10 '15 at 08:27

2 Answers2

0

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.

Air
  • 8,274
  • 2
  • 53
  • 88
user1557314
  • 169
  • 3
  • 14
0

Try this: <a\s+id="[^"]*"[^\/]\/>

$re = "/<a\\s+id=\"[^\"]*\"[^\\/]\\/>/mi"; 
$str = "<a id=\"8\" />\njdf\nsadfa\n<a id=\"df\" />\n<a name=\"df\" />"; 
$subst = ""; 

$result = preg_replace($re, $subst, $str);

And only remove <a id="8" /> then use <a\s+id="8"[^\/]\/>

Demo

Ahosan Karim Asik
  • 3,219
  • 1
  • 18
  • 27