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

  • Home
  • SEARCH
  • 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 1084707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:35:49+00:00 2026-05-16T22:35:49+00:00

I want to alter the class attribute of an image tag in PHP. I

  • 0

I want to alter the class attribute of an image tag in PHP. I could do it using regex but want to use DOM / SimpleXML. The simplexml element looks like this:

$obj = simplexml_load_string($unprocessedfield);
var_dump($obj->img);

object(SimpleXMLElement)#108 (1) { 
    ["@attributes"]=>  array(6) { 
        ["class"]=>  string(33) "alignnone size-full wp-image-5499" 
        ["title"]=>  string(22) "Image: credit" 
        ["src"]=>  string(28) "/files/2010/09/laser-338.jpg" 
        ["alt"]=>  string(0) "" 
        ["width"]=>  string(3) "338" 
        ["height"]=>  string(3) "338" 
    } 
} 

I want to add to the class attribute but can’t seem to access the individual elements of that array e.g. var_dump($obj->img->attributes['class']) gives me NULL.

thanks for any pointers.

  • 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-16T22:35:49+00:00Added an answer on May 16, 2026 at 10:35 pm

    I am not sure if this would work, but you could try the SimleXML->addAtribute() method and just add an atribute with the same name. In theory, it should overwrite the current attribute. But other than that I do not see a method to modify attributes through the manual.

    Perhaps the PHP DOM’s DOMElement::setAttribute() might work if the simpleXML one does not.

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

Sidebar

Related Questions

I want to append a <br /> to a particular class. Using the :after
I want to alter a record, but I don't want to read it out
I want to match both the src and title attributes of an image tag:
I want to send an Array with image paths and captions to a PHP
I am trying to alter the ins tag style attribute on html which looks
I want to create a new style, not just alter the style attribute of
I want to remove the class attribute from all elements that have an empty
Hi : I want to run an operation immediately after Guice hydrates my class
I want to alter a table from INNODB to MEMORY ENGINE. So I typed
I want to alter a table if that column already exists. If it doesn'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.