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?

Community
  • 1
  • 1
General_Twyckenham
  • 2,161
  • 2
  • 21
  • 36
  • 1
    yes it is an alternative, it is called querypath, http://querypath.org/ and it is awesome! – Twisted1919 Nov 17 '13 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?](http://stackoverflow.com/q/3577641) 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. – mario Nov 17 '13 at 22:40
  • Thanks! I'll look into those options. – General_Twyckenham Nov 17 '13 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. – P.J. Hinton Nov 18 '13 at 01:19

1 Answers1

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.

General_Twyckenham
  • 2,161
  • 2
  • 21
  • 36