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 6077477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:43:31+00:00 2026-05-23T10:43:31+00:00

First: I’ve read the general; don’t use RegEx on XHTML arguments like this one:

  • 0

First: I’ve read the general; don’t use RegEx on XHTML arguments like this one: RegEx match open tags except XHTML self-contained tags and I do understand how RegEx will fail on nested XHTML or XML nodes.

I don’t see why manipulating attributes of an XML alone should break using RegEx. So there seems to be exceptions to the general rule. Attributes are always contained in a single node starting with a < and ending with a > any other < or > in between would break the XML so such can’t occur.

Now I’d like to clean an XHTML string of any microdata it might contain. That is any attributes itemscope, itemtype, itemprop, itemid and itemref. Something like this:

...
<body itemscope="itemscope" itemtype="http://schema.org/WebPage">
<div itemprop="maincontent">content</div>
...

What’s the best way to do this in PHP?

  • 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-05-23T10:43:31+00:00Added an answer on May 23, 2026 at 10:43 am

    I’d actually suggest:

    1. Loading the string with something like SimpleXML.
    2. Removing the attributes you are interested in flushing.
    3. Saving it back to a string.

    There are a bunch of namespace issues that I’m not sure how you’d have to handle, but that will probably be cleaner/happier than trying to build one or more regex expressions and make sure you don’t miss anything.

    EDIT: turns out SimpleXML won’t work (limited modification capabilities) but DOM will. Something like this:

    $data=<<<END1
    <body itemscope="itemscope" itemtype="http://schema.org/WebPage">
    <div itemprop="maincontent">content</div>
    </body>
    END1;
    
    $xml=new DOMDocument();
    $xml->loadXML($data);
    
    // find every relevant node
    $xpath = new DOMXPath($xml);
    $attr = $xpath->query("//@itemscope|//@itemprop|//@itemtype");
    foreach ($attr as $entry) {
      $entry->parentNode->removeAttribute($entry->nodeName);
    }
    echo $xml->saveXML();
    

    You’d have to modify it to include all the attributes you want to remove, and like I said I have no clue how it would deal with namespaces, but its a start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, full disclosure: This is going towards a uni assignment, so I don't
First of all, my table structure is something similar like this: CREATE TABLE Testing(
First of all, I know I asked a similar question before but this one
First off, there's a bit of background to this issue available on my blog:
First let me say that I really feel directionless on this question. I am
First of all, I don't need a textual comparison so Beyond Compare doesn't do
First and foremost, I do not know RegEx but am trying to piece something
First of all, I read erickson's useful reply to Why can’t I define a
First of all, this is not schoolwork - just my curiousity as I for
First time posting here, will try to be succinct. This is a classic 'can't

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.