675 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+6 more
dexterous says

Hi All,

This might be too easy for someone who knows regex very well.. but I am not able to figure it out.

What have is big string with a lot of nodes in following format:

<curr v="USD"><curr v="HKD">
</curr></curr>

What I want is :

<curr v="USD"><curr v="HKD">
</curr></curr>

So that it becomes well formed XML . I am Using PHP by the way.

the attribute is always a capital V. So anything followed by V= should be changed to V=”same_value”

Is it something very tricky to do in regex? Any help is much appreciated.

EDIT : the post is not showing the preformatted text correctly. In the original string the quotes are missing, and I want the values to be wrapped in quotes.

296 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
theninja says
Nope, it’s pretty simple actually… you should check out Regexr, there’s an adobe air version too, it’s pretty handy for learning/building regular expressions if that’s not your forte.
There are a couple of ways of doing this, here’s one way :
(Press reply with quote to get the properly formatted version because it comes out as gobbledygook on here, as I’m sure you already noticed :D )

&lt;?php $data = '<curr V=US?&gt;<curr v="HKD"><curr v="GBP"><curr v="EUR"><curr v="AUD"><curr v="CNY">';

function add_quotes($matches) {
    return $matches[1].'"'.$matches[2].'"';
}

// added pre and htmlentities so it's displayed on the page
echo '<pre>'.htmlentities(preg_replace_callback('/(V=)([A-Z]{3})/', 'add_quotes', $data)).'</pre>'; 
?&gt;
<pre>

</pre></curr></curr></curr></curr></curr>
296 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
theninja says

Edit : Double post… damn browser :P

675 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+6 more
dexterous says

Thanks, I found another solution and that also worked, but your solution looks good.

One question – if I have more than 3 characters ( say, instead of currency codes if I have name of cities) then how to modify the {3} in your code to match n number of characters ?

296 posts
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
theninja says
One question – if I have more than 3 characters ( say, instead of currency codes if I have name of cities) then how to modify the {3} in your code to match n number of characters ?
Because city names are of a variable length (and I’m guessing could have spaces, dots or underscores/dashes) it’s slightly more complicated, but if the formatting’s the same as earlier, ie the city name is just before the >, you could just do this :

&lt;?php $data = '<city V=Londo?&gt;<city v="Hong" kong><city v="Washington" d.c.><city v="made-up-city">';

function add_quotes($match) {
    return '"'.$match[1].'"';
}

echo '<pre>'.htmlentities(preg_replace_callback('/(?<=V=)([\w\s-\.]+?)(?=>)/', 'add_quotes', $data)).'</pre>'; 
?&gt;
</city></city></city>
675 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+6 more
dexterous says

Thanks ninja,

That worked great – (though it did not work for washington d. c. – may be because it contained dots – after removing dots it worked)

Anyway I am not having any dots in the actual text that i am using so your solution is working good for me.

Many thanks.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by