Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7815559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:33:31+00:00 2026-06-02T05:33:31+00:00

Here is xml code, and I load this text as String and I need

  • 0

Here is xml code, and I load this text as String and I need all of its nested tags and content to be replaced with htmlentities..

<?xml version="1.0" encoding="utf-8"?>
<data>
<target><x id="25e02e3e839c-a1e6b03cb682"  pid="NLSheets" name="NLSheets" />Sheets"</target>
<target>"<x id="3510a371bdf8-861b965564ea" pid="NLTable" name="NLTable" />Table"</target>
<target>"<x id="48a1560eaa68-c400c8394f0a"  pid="NLCaption" name="NLCaption" />Caption"</target>
</data>

I have Used the following php code for the task.

function html_entities($matches) {
  return str_replace($matches[1], htmlentities($matches[1]), $matches[0]);
}

function get_tag( $tagname, $xml ) {
 $pattern = "/<$tagname ?.*>(.*?)<\/$tagname>/"; 
 $content = preg_replace_callback($pattern,  html_entities, $xml);
 return $content;
}

$content = get_tag('target', $str);

echo $content; 

Now the the problem is with the regular expression. I have used the regular expression

as you can see in get_tag function. $pattern = "/<$tagname ?.*>(.*?)<\/$tagname>/"; which will be built on run time as

/<target ?.*>(.*?)<\/target>

Now I am unable to fix the problem… The nested tags values are not converted to htmlentities.

Please help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:33:33+00:00Added an answer on June 2, 2026 at 5:33 am

    Change the line to this:

    $pattern = "/<$tagname ?.*?>(.*?)<\/$tagname>/"; 
    

    You need an extra non-greedy modifier to prevent the search for the closing part (>) of the opening tag from going too far and grabbing your inner content (and thus not making it available to the parenthetical grouping and thus htmlentities).

    We could improve this a little by using the ‘s’ modifier at the end to allow for newlines within the content (since the dot character doesn’t include newlines by default) as well as to prevent / within the opening tag while allowing newlines there, and allow any kind of whitespace to separate the element name from the attributes and to allow whitespace at the end of the closing tag:

    $pattern = "/<$tagname(?:\s[^\/]*?)?>(.*?)<\/$tagname\s*>/s";
    

    And shorten it:

    $pattern = "@<$tagname(?:\s[^/]*?)?>(.*?)</$tagname\s*>@s";
    

    It is for handling all these possible edge cases that it is safer to use an XML parser. For example, this would not catch:

    <target><![CDATA[ </target> ]]></target>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error in xml file very often. here is the code in
Here's the XML code I'm working with: <inventory> <drink> <lemonade supplier=mother id=1> <price>$2.50</price> <amount>20</amount>
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
Here is the code I have tried to append a node in existing xml,
enter code here I have a ListView that have a different Layout XML for
Here's my layout code; <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent> <TextView android:text=@string/welcome
Here is my code #!usr/bin/env perl # Setup includes use strict; use XML::RSS; use
I'm trying to update an element in the XML document below: Here's the code:
I have <.ImageView> enter code here s(icons) in my android layout xml file, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.