Reputation: 459
I have a <a>
tags in an xml file. I want to strip only <a>
tags.
For example:
<test>This is a line with <a name="idp173968"></a> tags in it</test>
I can't do str_replace
to replace tags since <a>
tag attribute is varied.
I tried:
preg_replace("/<a[^>]+\>/i", "", $content);
if the tags is structured like this <a name="idp173968"/>
, it works fine.
So,How to strip tags in my case?
Expected output:
<test>This is a line with tags in it</test>
Upvotes: 0
Views: 65
Reputation: 3868
<?php
$string = '<test>This is a line with <a name="idp173968"></a> tags in it</test>';
$pattern = '/<a\s.*?<\/a>/i';
echo preg_replace($pattern, "", $string);
?>
Upvotes: 0
Reputation: 26667
You can try a very simple regex like
<a\s.*?<\/a>
Example
echo preg_replace("/<a\s.*?<\/a>/i", "", $content);
=> <test>This is a line with tags in it</test>
$content="<test>This is a line with <a name=\"idp173968\"></a> tags in it</test><article-meta>asdf</article-meta>";
echo preg_replace("/<a\s.*?<\/a>/i", "", $content);
=><test>This is a line with tags in it</test><article-meta>asdf</article-meta>
Upvotes: 1