vidhya
vidhya

Reputation: 459

How to strip <a> tags in an xml file?

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_replaceto 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

Answers (2)

Priyank
Priyank

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

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can try a very simple regex like

<a\s.*?<\/a>

Regex Demo

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

Related Questions