tags:

views:

90

answers:

4

I need a simple markup language to store different parts of a string on a TEXT field, and then extract those parts. So basically I want some kind of simple XML. Storing those in the table field is easy, but extracting them... is other matter. I managed to do so using a simple regex done for regular HTML:

|<[^>]+>(.*)</[^>]+>|U

But in order to re-compose the original array (and use the markup more generally) I need also to know the tag names. And that regex does't do that.

Examples:

Input text:

<user_input>Hello! my name is Williams</user_input>

The preg_match_all() function using the above regex returns:

array
  0 => 
    array
      0 => string '<user_input>Hello! my name is Williams</user_input>' (length=34)

  1 => 
    array
      0 => string 'Hello! my name is Williams' (length=34)

I need it to return the "user_input" name of the tag. Yes, I know, I suck on regex. Yes, I know "use a XML parser", but that is too big for what I'am doing.

+1  A: 

So basically I want some kind of simple XML

Then you want an XML parser. And hey, PHP has an XML parsing extension you can install.

Seriously, trying to hack your way there with regexes is only going to end in pain and frustration. Use an XML parser, and save yourself hours of work.

but that is too big for what I'am doing.

No, it's not. You're wanting to parse something - hence, you should use a parser.

Anon.
It's interesting when people downvote something without leaving a comment. You've lost rep, the answer hasn't improved - what exactly have you achieved?
Anon.
I cosign recommending using an xml parser, but how about providing an example of using it to solve the op's issue? This would help him understand how it works instead of "RTFM" type responses.
meder
@meder: I'm not exactly a PHP guru - in order to post a working code sample, I'd have to install PHP, configure everything right, and test my code a bunch of times to make sure it works. I'd prefer to post a useful answer pointing the asker in the right direction, even if it is terse on copy-pastable code. That said, your answer is better than mine, and I have upvoted it for being correct and providing example usage.
Anon.
The parser is a nice solution, but I have already done the code for the regex (not just the method that parses the string, code around it too). If the number of users goes up and I have some issues scaling, I will probably refactor it to use the parser. Thanks anyway, I learned a new thing that will probably use on the future.
Diego
+6  A: 

How is a xml parser "too big"? PHP has built-in native functions that allow you to do it easily.

Regex doesn't fit the job.

<?php

$string = '
<root>
<input_name>blah</input_name>
</root>
';

$x = new DOMDocument();
$x->loadXML($string);
$root = $x->documentElement;
$elements = $root->getElementsByTagName('*');
$count = count($elements->length);

for ( $i = 0; $i< $count; $i++ ) {
    $el = $elements->item($i);
    echo $el->nodeName . '<br>';
    echo $el->nodeValue . '<br>';
}
meder
You can use simplexml as well.
meder
A: 
|<([^>]+)>(.*)</[^>]+>|U

Will do what you want. I merely added two parenthesis. It is a very brittle hack. You want to use a parser. Especially as you apparently don't understand regexps.

Turtle
A: 

Just use a capturing group like you did with the content:

|<([^>]+)>([^<]*)</\1>|

As an added bonus, you can use the captured name to make sure the closing tag has the same name.

Alan Moore
Thanks! Works perfectly!.
Diego