0

I have a question. I want to read a wrong xml format with the php function simplexml_load_string but I get empty values because the format is worng. Part of the xml:

<column name="price"><![CDATA[184.95]]></column>
<column name="category"><![CDATA[Dames]]></column>
<column name="subcategory"><![CDATA[Schoenen]]></column>
<column name="stock"><![CDATA[1]]></column>

I need to replace it to this:

<price><![CDATA[184.95]]></price>
<category><![CDATA[Dames]]></category>
<subcategory><![CDATA[Schoenen]]></subcategory>
<stock><![CDATA[1]]></stock>

to read the xml feed properly. Is it possible to replace this with preg_match for example? And how?

2
  • 1
    XSLT was defined for exactly that - converting one XML into another. Commented Sep 5, 2014 at 16:47
  • You do not necessarily get empty values only because of that XML. It looks fine. You probably don't know how to access it? Just commenting not that you ask the wrong way around and that you solve a programming problem in a way that works diametral against what you want to achieve (like using a regex in your accepted answer). Could it be? Commented Sep 8, 2014 at 12:20

2 Answers 2

1

How about:

$str = preg_replace('#<column name="([^"]+)">(.+?)</column>#', '<$1>$2</$1>', $str); 
Sign up to request clarification or add additional context in comments.

1 Comment

hi m42, this should be the solution but it isn't working. Even when i use regex tester regexpal.com it doesn't highlight the strings..
0

The below would do it, but I think you could probably do it with preg_replace, see this link: php preg_replace matches

preg_match_all('/<column name=\"([a-z]+)\">(.*)<\/column>/', $string, $matches);

$string = '';
foreach( $matches[1] as $key => $name )
{
    $string .= "<{$name}>{$matches[2][$key]}</{$name}>\n";
}

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.