2

I'm trying to design an interface to change <img> sources throughout my website, so that I don't need to change the sources by hand anytime we want to change what images my site displays.

I looked at DOMDocument already, and while it's useful for accessing the different elements in a file, it wraps the files in it's own HTML tags, which messes up the files (I already tried looking here but those solutions didn't work for me)

Is there a better way to take a file, retrieve element information and attributes (like src and id) from img tags, edit those elements, and save the file?

4
  • 1
    yes it is an alternative, it is called querypath, querypath.org and it is awesome! Commented Nov 17, 2013 at 22:39
  • 1
    Using plain DomDocument is often a subpar code overhead. Look into How do you parse and process HTML/XML in PHP? on more useful wrappers atop. With QP you can often just use qp($html)->find("img")->attr("src", "img2.png")->top("body")->html(); to get just the adapted inner part. Commented Nov 17, 2013 at 22:40
  • Thanks! I'll look into those options. Commented Nov 17, 2013 at 23:28
  • If the IMG elements in the document remain the same, and you need only make adjustments to the attributes on the element, you might want to consider converting the HTML page into a template, using a templating library of your choice and then storing the values of the elements' attributes in the database. Some examples of PHP libs that come to mind are Smarty and Twig. Commented Nov 18, 2013 at 1:19

1 Answer 1

1

Just to close this up - I ended up using Querypath to accomplish this task. It works pretty well and was pretty simple to utilize.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.