Regex – PHP Str_replace XML + WordPress Issue

ლ(ಠ益ಠლ)

#1 DOUBT:

Read More

I got this line from my XML:

<foto>http://localhost:8090/wp-content/uploads/2013/03/DSC01132.jpg</foto>

I need to transform that in this:

<foto path=”http://localhost:8090/wp-content/uploads/2013/03/” arquivo=”DSC01132.jpg” 
titulo=”DSC01132” />

How could I do that via regex and PHP? I’m opening XML file via:

$data = file_get_contents("1439828483.xml");

Is that even possible? Just to mention, this is the scheme I’m using:

    <?
    $data = file_get_contents("1439828483.xml");
    $data = str_replace("<title></title>", "", $data);
    file_put_contents("newfile.xml", $data);
    ?>

#2 DOUBT:

This one….. man….. (ノಠ益ಠ)ノ彡┻━┻**

I need to create a custom PHP function to get info from a metadata.
I’m running WordPress + Real Estate Theme, but the theme gallery do not attach images/pictures in default WordPress gallery. So it creates a meta data that refer to a link in other metakey via post_id, as you can see here:
enter image description here

So when I select REAL_HOMES_property_image field to show me picture URL, it just shows me: 10462 in xml tag:

<REAL_HOMES_property_images><![CDATA[10462]]></REAL_HOMES_property_images>

and this should actually looks like this:

<REAL_HOMES_property_images><![CDATA[http://localhost:8090/wp-content/uploads/2015/06/IMG_3201.jpg]]></REAL_HOMES_property_images>

Thanks ( ͡ᵔ ͜ʖ ͡ᵔ )


People are telling me to use PHP XML Dom parser, but as I’m not used to it, how my current file would be using this Dom parser? My actual file looks like this:

//open file and get data
$data = file_get_contents("1439828483.xml");

// do tag replacements or whatever you want
$data = str_replace("&nbsp;", "", $data);
$data = str_replace("data>", "imoveis>", $data);
$data = str_replace("post>", "imovel>", $data);
$data = str_replace("<image>", "", $data);
$data = str_replace("</image>", "", $data);
$data = str_replace("file>", "foto>", $data);
$data = str_replace("<caption></caption>", "", $data);
$data = str_replace("<description></description>", "", $data);
$data = str_replace("<alt></alt>", "", $data);
$data = preg_replace('#(<title>).*?(</title>)#', '$1$2', $data);
$data = str_replace("<title></title>", "", $data);
$data = preg_replace('#(<id>).*?(</id>)#', '$1$2', $data);
$data = str_replace("<id></id>", "<endereco_cep></endereco_cep>", $data);
$data = preg_replace("/ssssssssss/", "",$data);
$data = str_replace("<modalidade><![CDATA[venda]]></modalidade>", "<modalidade><![CDATA[1]]></modalidade>",$data);
$data = str_replace("<modalidade><![CDATA[aluguel]]></modalidade>", "<modalidade><![CDATA[3]]></modalidade>",$data);
$data = str_replace("<tipo><![CDATA[apartamento]]></tipo>", "<tipo><![CDATA[1]]></tipo>",$data);
$data = str_replace("<tipo><![CDATA[casa]]></tipo>", "<tipo><![CDATA[4]]></tipo>",$data);
$data = str_replace("<tipo><![CDATA[galpao]]></tipo>", "<tipo><![CDATA[12]]></tipo>",$data);
$data = str_replace("<tipo><![CDATA[chacara]]></tipo>", "<tipo><![CDATA[10]]></tipo>",$data);
$data = str_replace("<tipo><![CDATA[sitio]]></tipo>", "<tipo><![CDATA[10]]></tipo>",$data);
$data = str_replace("<tipo><![CDATA[lote]]></tipo>", "<tipo><![CDATA[5]]></tipo>",$data);
$data = str_replace("<tipo><![CDATA[ponto-comercial]]></tipo>", "<tipo><![CDATA[8]]></tipo>",$data);
$data = str_replace('<em id="__mceDel"><em id="__mceDel">', "",$data);

$data = preg_replace('#(<bairro><![CDATA[)[^>]*?>([^>]*?><)#', '$1$2', $data);
while(preg_match('#(<bairro>[^->]*?)-([^->]*?-)*([^->]*?'.'>)#', $data)) {
    $data = preg_replace('#(<bairro>[^->]*?)-(([^->]*?-)*)([^->]*?'.'>)#', '$1 $2$4', $data);
}

$data = preg_replace('#(<cidade><![CDATA[[^>]*?)>[^>]*?(]]><)#', '$1$2', $data);
while(preg_match('#(<cidade>[^->]*?)-([^->]*?-)*([^->]*?'.'>)#', $data)) {
    $data = preg_replace('#(<cidade>[^->]*?)-(([^->]*?-)*)([^->]*?'.'>)#', '$1 $2$4', $data);
}
//save it back:
file_put_contents("newfile.xml", $data);

I started a new PHP file with this in order to work with this DOM XML parser:

//open file and get data
$xmlDoc = new DOMDocument();
$xmlDoc->load("1439828483.xml");

// do tag replacements or whatever you want


//save it back:
print $xmlDoc->save("newfile.xml");

Is that right?

Related posts

2 comments

  1. You should create for two doubts two different questions, since they seem to be different topics.

    Anyway, I’ll answer your first doubt, by proposing a regex like this:

    <.*?>(.*)/(w+)(.*?)<.*?>
    

    Working demo

    However, keep in mind that it’s a bad idea to parse xml/html with regex since as you can see in your own posts… weird characters can mess up regex engines.

  2. The best way to solve XML issue is to use a XML Parser library.

    I chose the SimpleXML because it’s simple? 😛

    So, this was the scheme I used:

    //open file and get data
    $xml=simplexml_load_file("1439828483.xml") or die("Error: Cannot create object");
    
    // make a loop to run over the xml
    for ($i = 0; $i < count($xml); $i++) {
    // this way I can manage what is inside the tag handling as it was a PHP string.
    $id = $xml->post[$i]->id;
    }
    //Save the file
    $xml->asXML("newfile.xml");
    

    That’s it (☞゚ヮ゚)☞

Comments are closed.