How to capture all images from an image field in XML to write to MYSQL?

Asked

Viewed 60 times

0

Good Afternoon!

I need to capture in PHP all the images contained in the XML image field below to write to MYSQL, as I do?

<Media>
<Item medium="image">http://teste.com.br/11772/21973974.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973975.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973976.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973977.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973978.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973979.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973980.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973981.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973982.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973983.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973984.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973985.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973986.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973987.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973988.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973989.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973990.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973991.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973992.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973993.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973994.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973995.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973996.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973997.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973998.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21973999.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974000.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974001.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974002.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974003.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974004.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974005.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974006.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974007.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974008.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974009.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974010.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974011.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974012.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974013.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974014.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974015.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974016.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974017.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974018.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974019.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974020.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974021.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974022.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974023.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974024.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974025.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974026.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974027.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974028.jpg</Item>
<Item medium="image">http://teste.com.br/11772/21974029.jpg</Item>
</Media>

I’m waiting for help!

  • Could explain better what it means to write to mysql?

  • @durtto want to store in a variable and record all images in the database.

  • You want to save the image file or the image url?

  • @durtto the image url. But this is simple, I need to know how to capture all these images.

2 answers

0

There are several ways.

The quickest way to do it, at least for me, would be to use the preg_match_all(), read about it here.

That way it would be:

preg_match_all('/<Item medium="image">(.*?)<\/Item>/', $texto, $url);

This would return an array $url with all links, which are between the tags of Item.

This would be the return using the above function:

Array ( [0] => Array ( [0] => http://teste.com.br/11772/21973974.jpg [1] => http://teste.com.br/11772/21973975.jpg [2] => http://teste.com.br/11772/21973976.jpg [3] => http://teste.com.br/11772/21973977.jpg [4] => http://teste.com.br/11772/21973978.jpg [5] => http://teste.com.br/11772/21973979.jpg [6] => http://teste.com.br/11772/21973980.jpg [7] => http://teste.com.br/11772/21973981.jpg [8] => http://teste.com.br/11772/21973982.jpg [9] => http://teste.com.br/11772/21973983.jpg [10] => http://teste.com.br/11772/21973984.jpg [11] => http://teste.com.br/11772/21973985.jpg [12] => http://teste.com.br/11772/21973986.jpg [13] => http://teste.com.br/11772/21973987.jpg [14] => http://teste.com.br/11772/21973988.jpg [15] => http://teste.com.br/11772/21973989.jpg [16] => http://teste.com.br/11772/21973990.jpg [17] => http://teste.com.br/11772/21973991.jpg [18] => http://teste.com.br/11772/21973992.jpg [19] => http://teste.com.br/11772/21973993.jpg [20] => http://teste.com.br/11772/21973994.jpg [21] => http://teste.com.br/11772/21973995.jpg [22] => http://teste.com.br/11772/21973996.jpg [23] => http://teste.com.br/11772/21973997.jpg [24] => http://teste.com.br/11772/21973998.jpg [25] => http://teste.com.br/11772/21973999.jpg [26] => http://teste.com.br/11772/21974000.jpg [27] => http://teste.com.br/11772/21974001.jpg [28] => http://teste.com.br/11772/21974002.jpg [29] => http://teste.com.br/11772/21974003.jpg [30] => http://teste.com.br/11772/21974004.jpg [31] => http://teste.com.br/11772/21974005.jpg [32] => http://teste.com.br/11772/21974006.jpg [33] => http://teste.com.br/11772/21974007.jpg [34] => http://teste.com.br/11772/21974008.jpg [35] => http://teste.com.br/11772/21974009.jpg [36] => http://teste.com.br/11772/21974010.jpg [37] => http://teste.com.br/11772/21974011.jpg [38] => http://teste.com.br/11772/21974012.jpg [39] => http://teste.com.br/11772/21974013.jpg [40] => http://teste.com.br/11772/21974014.jpg [41] => http://teste.com.br/11772/21974015.jpg [42] => http://teste.com.br/11772/21974016.jpg [43] => http://teste.com.br/11772/21974017.jpg [44] => http://teste.com.br/11772/21974018.jpg [45] => http://teste.com.br/11772/21974019.jpg [46] => http://teste.com.br/11772/21974020.jpg [47] => http://teste.com.br/11772/21974021.jpg [48] => http://teste.com.br/11772/21974022.jpg [49] => http://teste.com.br/11772/21974023.jpg [50] => http://teste.com.br/11772/21974024.jpg [51] => http://teste.com.br/11772/21974025.jpg [52] => http://teste.com.br/11772/21974026.jpg [53] => http://teste.com.br/11772/21974027.jpg [54] => http://teste.com.br/11772/21974028.jpg [55] => http://teste.com.br/11772/21974029.jpg ) [1] => Array ( [0] => http://teste.com.br/11772/21973974.jpg [1] => http://teste.com.br/11772/21973975.jpg [2] => http://teste.com.br/11772/21973976.jpg [3] => http://teste.com.br/11772/21973977.jpg [4] => http://teste.com.br/11772/21973978.jpg [5] => http://teste.com.br/11772/21973979.jpg [6] => http://teste.com.br/11772/21973980.jpg [7] => http://teste.com.br/11772/21973981.jpg [8] => http://teste.com.br/11772/21973982.jpg [9] => http://teste.com.br/11772/21973983.jpg [10] => http://teste.com.br/11772/21973984.jpg [11] => http://teste.com.br/11772/21973985.jpg [12] => http://teste.com.br/11772/21973986.jpg [13] => http://teste.com.br/11772/21973987.jpg [14] => http://teste.com.br/11772/21973988.jpg [15] => http://teste.com.br/11772/21973989.jpg [16] => http://teste.com.br/11772/21973990.jpg [17] => http://teste.com.br/11772/21973991.jpg [18] => http://teste.com.br/11772/21973992.jpg [19] => http://teste.com.br/11772/21973993.jpg [20] => http://teste.com.br/11772/21973994.jpg [21] => http://teste.com.br/11772/21973995.jpg [22] => http://teste.com.br/11772/21973996.jpg [23] => http://teste.com.br/11772/21973997.jpg [24] => http://teste.com.br/11772/21973998.jpg [25] => http://teste.com.br/11772/21973999.jpg [26] => http://teste.com.br/11772/21974000.jpg [27] => http://teste.com.br/11772/21974001.jpg [28] => http://teste.com.br/11772/21974002.jpg [29] => http://teste.com.br/11772/21974003.jpg [30] => http://teste.com.br/11772/21974004.jpg [31] => http://teste.com.br/11772/21974005.jpg [32] => http://teste.com.br/11772/21974006.jpg [33] => http://teste.com.br/11772/21974007.jpg [34] => http://teste.com.br/11772/21974008.jpg [35] => http://teste.com.br/11772/21974009.jpg [36] => http://teste.com.br/11772/21974010.jpg [37] => http://teste.com.br/11772/21974011.jpg [38] => http://teste.com.br/11772/21974012.jpg [39] => http://teste.com.br/11772/21974013.jpg [40] => http://teste.com.br/11772/21974014.jpg [41] => http://teste.com.br/11772/21974015.jpg [42] => http://teste.com.br/11772/21974016.jpg [43] => http://teste.com.br/11772/21974017.jpg [44] => http://teste.com.br/11772/21974018.jpg [45] => http://teste.com.br/11772/21974019.jpg [46] => http://teste.com.br/11772/21974020.jpg [47] => http://teste.com.br/11772/21974021.jpg [48] => http://teste.com.br/11772/21974022.jpg [49] => http://teste.com.br/11772/21974023.jpg [50] => http://teste.com.br/11772/21974024.jpg [51] => http://teste.com.br/11772/21974025.jpg [52] => http://teste.com.br/11772/21974026.jpg [53] => http://teste.com.br/11772/21974027.jpg [54] => http://teste.com.br/11772/21974028.jpg [55] => http://teste.com.br/11772/21974029.jpg ) ) 

So to get the "separate" values just use the array itself said above, obviously. Using $url[1][0], for example.

You can also make a foreach of $url[1], so to display each link or insert or join to an INSERT. Finally.

  • Handle xml with regex O.o?

  • 1

    He just wants to get the link. That’s an output. Not ideal. If you really want to take advantage of XML you can use the one simplexml and loop the path of items, I’ve even answered questions using this, but I don’t think it’s fast (to do) than regex. But if XML has this pattern I think there are no problems. Obviously, if there are several "<media>", this will not do, but this was not mentioned.

0

I don’t know if you’ve had your answer, but here’s an extra solution:

Use this function to read your xml file and turn it into a php array. The xml file can have any structure. Never mind. The function will generate a php array that will mirror your xml. Source: link.

/*
This function goes to the directory $pathDir, pass trough each all files inside it,
seeks for any file that math to the $searchString and reads it.
Return the content of all matched xml files within a php Array.
Ex: 
We have inside the FTP the follow files genereted by a CronJob: 
fileCarAudiRent2015.xml, fileCarAudiRent2016.xml, fileCarAudiSell2015.xm,
fileCarBMWRent2015.xml, fileCarBMWSell2016.xml, fileBoatWMBRent2015.xml, fileBoatWMBSell2015.xml,
fileBoatAudiRent2015.xml, fileBoatAudiSell2016.xml, ... .

We want to grab the content of all files that matches "Boat" string.
So, we need to do like this:
$arrInfo = read_XML_from_Folder_return_arrayAll("c:/Path_to_the_Folder","Boat");

We want to grab the content of all files that matches "Sell2015" string.
So, we need to do like this:
$arrInfo = read_XML_from_Folder_return_arrayAll("c:/Path_to_the_Folder","Sell2015");

We should inprove this function to match criteria like:
Give me all that has Sell AND 2015
Give me all that has Sell OR 2015
...
Give me all that has BMW AND 2015
*/


public function read_XML_from_Folder_return_arrayAll($pathDir,$searchString) {

$xml_array="";
$dir = new DirectoryIterator($pathDir); //http://php.net/manual/en/class.directoryiterator.php
foreach ($dir as $fileinfo) {
  if ($fileinfo->isFile()) {
      $fileName = $fileinfo->getFilename();
      //Macth rule
      $pos = strpos($fileName, $searchString);
      if ( $pos!== false) {//If match the sought string
        $filePath = $pathDir.$fileName;
        $xml = simplexml_load_file($filePath);
            $xml_array [] = unserialize(serialize(json_decode(json_encode((array) $xml), 1)));  

           /* If needed 
           $invoice_date = $xml_array["Documents"]["InvoiceDate"];
           $date = DateTime::createFromFormat('dmY', $invoice_date);//Formated date inside xml
           $year = $date->format('Y');//Desired output year
           $month = $date->format('M');//Desired output month 
           */

      }//End of if ( $pos!== false)                        
  } 
}
//Testing
echo "<pre>";
  print_r($xml_array);
echo "</pre>";


return $xml_array;

 }

So the function accepts 2 parameters:

  1. $pathDir = the path to the FOLDER where the xmls files are located
  2. $searchString = a string to be filtered in the filename.

Let’s say that inside Folder Ftp_xmls vc you have different xml files. You only want the files from me.xml. Then the function enters this Folder, and returns a php array with data from all the xmls they have "pictures" in the name.

$arrPHP =  read_XML_from_Folder_return_arrayAll("c:/path/dir ...","fotos");

Now you have a php array with what interests you. Back to your dog, loop this array and insert the information you want into Mysql.

Vc tbm will need to filter your string before inserting in Mysql, because you will get error if you try without the filter.

For this use this function within your loop. Source: in that link.

private function tool_string_filter($str) {
    if(is_null($str)){
     return "NULL";
    }
    else{
      if(is_numeric ($str)){
           return $str;
      }
      else{
           return "\"".mysql_escape_string(utf8_encode($str))."\"";
      }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.