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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:58:20+00:00 2026-05-27T09:58:20+00:00

A PHP regex / PHP DOM / PHP XPath question. Given the following HTML

  • 0

A PHP regex / PHP DOM / PHP XPath question.

Given the following HTML with inline CSS:

<p style='text-indent: 22px; font-weight: bold; line-height: 1em; color: #FFF'>

How do I remove the ‘line-height’ and ‘color’ CSS properties, and leave text-indent and font-weight untouched, so the resultant HTML is:

<p style='text-indent: 22px; font-weight: bold;'>

The HTML file could be potentially hundreds of lines, with various nesting of tags and other attributes applied to any tag.

Note that the ‘style’ attribute may be applied to other tags than <p>

I am aware there are approaches using both PHP DOM and regex – my current thinking was using something along these lines:

$elements = $xPath->query('//*[@style="color"]');
foreach ($elements as $element) {   
  //remove style='color'
}

Many thanks

EDIT

Here’s my solution:

https://github.com/sabberworm/PHP-CSS-Parser

To create:

$dom = new DOMDocument;
@$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
$xPath = new DOMXPath($dom);
$elements = $xPath->query('//p|//span');
foreach($elements as $element){
    $oParser = new CSSParser("p{" . $element->getAttribute('style') . "}");
    $oCss = $oParser->parse();
    foreach($oCss->getAllRuleSets() as $oRuleSet) {
        $oRuleSet->removeRule('line-');
        $oRuleSet->removeRule('margin-');
        $oRuleSet->removeRule('font-');
    }
    $css = $oCss->__toString();
    $css = substr_replace($css, '', 0, 3);
    $css = substr_replace($css, '', -1, 1);
    $element->setAttribute('style', $css);
}
$src = $dom->saveHTML();
  • 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-27T09:58:21+00:00Added an answer on May 27, 2026 at 9:58 am

    Definitely use proper HTML and CSS parsers rather than regexes. For the XPath query, use the contains function to find the nodes to alter:

    //*[contains(@style, 'color:')]
    

    Then use a CSS parser to remove the properties you don’t want.

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

Sidebar

Related Questions

Possible Duplicates: How do I extract HTML content using Regex in PHP RegEx match
The x modifier code in this tutorial Php regex tutorial gives me the following
i need to find inline javascript with php regex. I can find external script,
I've been using the following site to test a PHP regex so I don't
iv got a question in php regex. For example if we use <?php $str
I am trying to parse text for email id's using php / regex. Are
Duplicate: PHP validation/regex for URL My goal is create a PHP regex for website
I read this PHP RegEx page , but either I'm missing something, misreading something,
Does anyone know a PHP RegEx to allow only relative paths, not absolute? For
I need a PHP Regex that can parse .strings files. In particular, it needs

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.