1

ლ(ಠ益ಠლ)

#1 DOUBT:

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("/\s\s\s\s\s\s\s\s\s\s/", "",$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?

4
  • 1
    Wow, nice smilies... :) Why don't you use a dedicated DOM parser to add attributes? Commented Aug 21, 2015 at 15:57
  • 1
    don't use string operations on xml. use a DOM parser. Commented Aug 21, 2015 at 15:58
  • How does it work? People tell me to use own PHP XML but I have no idea how. I have been using this method and got almost everything done. Using DOM Parser I don't need to use Regex to catch stuff? Can someone give me an example? Also.... my #2 doubt probably is not a xml "issue". Any idea how to solve it? Commented Aug 21, 2015 at 16:15
  • 1
    Please keep each question as a single question. Your second point appears to be completely unrelated to the first, so just edit it out of here and ask a new question. Commented Aug 22, 2015 at 13:38

2 Answers 2

2

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.

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

3 Comments

Thanks! Actually after posting this question and people above telling me to use DOM Parser I found a PHP Library called SimpleXML. It's so simple that even me being a php beginner can work with it :D As simplexml can't rename xml tags without custom functions, I'm working with it and then save the file and open using file_get_contents and str_replace :D Now for the #2 doubt, do you have any idea how to solve it?
@Diego SimpleXML seems a good choice. You should post your own answer and mark it as resolved
Oh cool! I will do this tomorrow. I got the code in my office notebook. Thank you again :D Only one question: I'm editing using SimpleXML but when I change the xml tag value, it removes CDATA "layout". Can it affect how a 3rd party script works? I mean... Can it refuse to accept my modified xml file because it doesn't have the CDATA layout in some fields? I tried to recover it, but as soon as I use something like: xml->post = "<![CDATA[". $id ."]]>" it becomes ascii code...
0

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

I chose the SimpleXML because it's simple? :P

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

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.