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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:13:39+00:00 2026-05-23T06:13:39+00:00

For a system I am building I am defining a general style stored in

  • 0

For a system I am building I am defining a general style stored in LINKSTYLE that should be applied to a elements that are not yet styled (inline). I am not very experienced with the DOMDocument or xpath and I can’t figure out what is going wrong.

Thanks to Gordon I’ve updated my code:

libxml_use_internal_errors(true);    

$html  = '<a href="#">test</a>'.
         '<a href="#" style="border:1px solid #000;">test2</a>';

$dom    = new DOMDocument();
$dom->loadHtml($html);
$dom->normalizeDocument();  
$xpath = new DOMXPath($dom);

foreach($xpath->query('//a[not(@style)]') as $node)
    $node->setAttribute('style','border:1px solid #000');

return $html;

With this updated code I receive no more errors, however the a element does not get styled.

  • 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-23T06:13:40+00:00Added an answer on May 23, 2026 at 6:13 am

    Use libxml_use_internal_errors(true) to suppress parsing errors stemming from loadHTML.

    • libxml_use_internal_errors() — Disable libxml errors and allow user to fetch error information

    The XPath query is invalid because contains expects a value to search for in the style attribute.

    • fn:contains($arg1 as xs:string?, $arg2 as xs:string?) as xs:boolean

    If you want to find all anchors without a style element, just use

    //a[not(@style)]
    

    You are not seeing your changes, because you are returning the string stored in $html. Once you loaded the string with DOMDocument, you have to serialize it back after you have have run your query and modified the DOMDocument’s internal representation of that string.

    Example (demo)

    $html = <<< HTML
    <ul>
        <li><a href="#foo" style="font-weight:bold">foo</a></li>
        <li><a href="#bar">bar</a></li>
        <li><a href="#baz">baz</a></li>
    </ul>
    HTML;
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $xp = new DOMXpath($dom);
    foreach ($xp->query('//a[not(@style)]') as $node) {
        $node->setAttribute('style', 'font-weight:bold');
    }
    echo $dom->saveHTML($dom->getElementsByTagName('ul')->item(0));
    

    Output:

    <ul>
    <li><a href="#foo" style="font-weight:bold">foo</a></li>
        <li><a href="#bar" style="font-weight:bold">bar</a></li>
        <li><a href="#baz" style="font-weight:bold">baz</a></li>
    </ul>
    

    Note that in order to use saveHTML with an argument, you need at least PHP 5.3.6.

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

Sidebar

Related Questions

I'm building a system that will create a tournament based on a list of
We're building an n-tier system based on WCF (but not Entity Framework) and we've
I am building a multithreaded system that works like this: While there are entities:
I'm building a plugin system for my application. I've read that anyone can decomple
I'm building a system using django, Sphinx and MySQL that's very quickly becoming quite
I am building a system that stores articles and tags that categorize the article.
I'm designing/building a system of classes that all derive from a single base class.
How to know that, the system you are building is a better as Desktop
I'm building a system of apps that listen for files dumped into a folder
I'm building a system that need to collect some user sensitive data via secured

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.