This is a discussion on help - isolate specific xml from mySQL entry? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I wonder if its possible to "extract"/copy/parse a specific line of xml from my mySQL table - ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I wonder if its possible to "extract"/copy/parse a specific line of xml
from my mySQL table - isolate it from the rest of the mysql entry (on PHP display)? example (as mySQL see's it): "Today is sunny <img src="/pictures/sun.gif" /> with chances of rain <img src="/pictures/raincloud.jpg" width="50" alt="rain" title="rain" /> this evening. So you may want to carry your umbrella on your way to work." isolated out to: <img src="/pictures/sun.gif" /><img src="/pictures/raincloud.jpg" width="50" alt="rain" title="rain" /> In this example I'm trying to make a PHP file that isolates and displays only the img src tags, ignoring the text spiel attached to it. Of course I can get the whole entry from mySQL to pop-up in the PHP (all the regular text + the images), but I have yet to figure out a way just to have the images showing (since they share the same mySQL table, I can't isolate it normally as if it were 2 different columns/tables). |
|
|||
|
henshu wrote:
> I wonder if its possible to "extract"/copy/parse a specific line of xml > from my mySQL table - isolate it from the rest of the mysql entry (on > PHP display)? > > example (as mySQL see's it): > "Today is sunny <img src="/pictures/sun.gif" /> with chances of rain > <img src="/pictures/raincloud.jpg" width="50" alt="rain" title="rain" /> > this evening. So you may want to carry your umbrella on your way to work." > > isolated out to: > <img src="/pictures/sun.gif" /><img src="/pictures/raincloud.jpg" > width="50" alt="rain" title="rain" /> I do not believe that will be possible using only SQL, and I'm not even sure it's adviseable. You will probably need to do some post processing with PHP. It can probably be done using regex, or you can use XML DOM functions to parse the XML, and extract all the child nodes using for ex. XPATH. -- Tommy http://design.twobarks.com/ |
|
|||
|
"henshu" <henshu_san@hotmail.com> wrote in message news:CgILf.108$8d1.4@read1.cgocable.net... >I wonder if its possible to "extract"/copy/parse a specific line of xml >from my mySQL table - isolate it from the rest of the mysql entry (on PHP >display)? > > example (as mySQL see's it): > "Today is sunny <img src="/pictures/sun.gif" /> with chances of rain <img > src="/pictures/raincloud.jpg" width="50" alt="rain" title="rain" /> this > evening. So you may want to carry your umbrella on your way to work." > > isolated out to: > <img src="/pictures/sun.gif" /><img src="/pictures/raincloud.jpg" > width="50" alt="rain" title="rain" /> <?php //extract images from XHTML string $contents="Today is sunny <img src=\"/pictures/sun.gif\" /> with chances of rain <img src=\"/pictures/raincloud.jpg\" width=\"50\" alt=\"rain\" title=\"rain\" /> this evening. So you may want to carry your umbrella on your way to work."; $doc = DOMDocument::loadHTML($contents); //could be loadXML(), but it requires the header stuff. loadHTML() is more forgiving $doc->saveHTML(); //turns into a proper HTML document $imgs=$doc->getElementsByTagName("img"); foreach ($imgs as $img) { echo "<img src=\"".$img->getAttribute("src")."\""; if ($img->getAttribute("width")) { echo " width=\"".$img->getAttribute("width")."\"";} if ($img->getAttribute("height")) { echo " height=\"".$img->getAttribute("height")."\"";} if ($img->getAttribute("alt")) { echo " alt=\"".$img->getAttribute("alt")."\"";} if ($img->getAttribute("title")) { echo " title=\"".$img->getAttribute("title")."\"";} if ($img->getAttribute("align")) { echo " align=\"".$img->getAttribute("align")."\"";} if ($img->getAttribute("hspace")) { echo " hspace=\"".$img->getAttribute("hspace")."\"";} if ($img->getAttribute("vspace")) { echo " vspace=\"".$img->getAttribute("vspace")."\"";} if ($img->getAttribute("ismap")) { echo " ismap=\"".$img->getAttribute("ismap")."\"";} if ($img->getAttribute("usemap")) { echo " usemap=\"".$img->getAttribute("usemap")."\"";} echo " />\n"; } ?> there may be other attributes like id, name, border, align. > > > In this example I'm trying to make a PHP file that isolates and displays > only the img src tags, ignoring the text spiel attached to it. Of course I > can get the whole entry from mySQL to pop-up in the PHP (all the regular > text + the images), but I have yet to figure out a way just to have the > images showing (since they share the same mySQL table, I can't isolate it > normally as if it were 2 different columns/tables). |