1

I have a form something like this .

 <form id="formPropiedades" name="formPropiedades" method="post" action="./index.php">
        <table width="70%" cellpadding="2" border="1" style="margin: 0 auto;margin-bottom:70px" class="parametros">
            <caption>
                <h1>Parámetros generales de la plantilla</h1>
            </caption>
            <tbody>
                <tr>
                    <th colspan="2">Campo</th>
                    <th colspan="6">Filtro</th>
                </tr>
                <tr class="row">
                    <td class="dbList" colspan="2">Address</td>
                    <td colspan="6">
                        <input type="text" style="width: 100%;border: solid 1px black;" name="inp[]" value="">
                        <select id="tablaCampo1" class="tablaCampo1" name="tablaCampo1">
<option value="SALTEST.ANUNCIO.ID_CIUDAD">SALTEST.ANUNCIO.ID_CIUDAD</option>
                            <option value="SALTEST.ANUNCIO.CIUDAD">SALTEST.ANUNCIO.CIUDAD</option>
                            <option value="SALTEST.ANUNCIO.MUNICIPIO">SALTEST.ANUNCIO.MUNICIPIO</option>
                            <option value="SALTEST.ANUNCIO.ID_ESTADO">SALTEST.ANUNCIO.ID_ESTADO</option>
                            <option value="SALTEST.ANUNCIO.ESTADO">SALTEST.ANUNCIO.ESTADO</option>
                            <option value="SALTEST.ANUNCIO.ID_MUNICIPIO">SALTEST.ANUNCIO.ID_MUNICIPIO</option>

                        </select>
                        <button class="makeStrng" type="button">+</button>
                    </td>
                </tr>
                <tr>
                    <td align="right" colspan="8">
                        <input type="button" id="guardarBtn" value="guardar" name="guardar" style=" float:left">
                        <input type="button" id="step1" value="prev" name="prev">
                        <input type="submit" id="propiedades" value="Enviar" name="submitme">
                    </td>
                </tr>
            </tbody>
        </table>
    </form>

So the option in the select , are actually columns from an XML. That has the data for the respective field

To give you an Idea ,The xml looks something like this

<SALTEST>

<ANUNCIO>
    <CIUDAD>Some Name</CIUDAD>
    <MUNICIPIO>SOme Name </MUNICIPIO>
    <ESTADO>Some name </ESTADO>
</ANUNCIO>
<ANUNCIO>
    <CIUDAD>Some Name</CIUDAD>
    <MUNICIPIO>SOme Name </MUNICIPIO>
    <ESTADO>Some name </ESTADO>
</ANUNCIO>

<SALTEST>

The user will use the select option and create a string that will have the setting to create the content

For example

to create the address string user will save a setting some thing like this .

String1 :

<fld>SALTEST.ANUNCIO.ESTADO </fld><fld>SALTEST.ANUNCIO.MUNICIPIO </fld><fld>SALTEST.ANUNCIO.CIUDAD</fld>

STRING2

 <fld>SALTEST.ANUNCIO.ESTADO</fld><ltr>,</ltr> <fld>SALTEST.ANUNCIO.MUNICIPIO</fld><ltr>,</ltr><fld>SALTEST.ANUNCIO.CIUDAD</fld>

STRING3 :

<fn>if(<fld>SALTEST.ANUNCIO.ID_CIUDAD</fld>,<fld>SALTEST.ANUNCIO.ID_CIUDAD</fld>,"<",<fld>SALTEST.ANUNCIO.ID_CIUDAD</fld>)</fn>

So basically the string will have three types of separators.

1.<fld></fld> These are the fields are are to be used from XML .

2.<fn></fn> ANything between these is a function and I am telling the system to detect it as a function

3.<ltr></ltr> ANything between these are characters that has to be used to separate each

for example :

 <fld>SALTEST.ANUNCIO.ESTADO</fld><ltr>,</ltr> <fld>SALTEST.ANUNCIO.MUNICIPIO</fld><ltr>,</ltr><fld>SALTEST.ANUNCIO.CIUDAD</fld>

Should give SOme Name , Some Name ,Some name .

I have to write a PHP script to detect each type of string and then apply related functionality

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130
  • Don't do string manipulation. Use an XML DOM parser to parse `STRING2` and `STRING3`. – Barmar Mar 12 '15 at 06:44
  • @Barmar but these are just normal string and the tags like `` etc are just hypothetical . I created just now to explain my concept better . But I am going to look into the XM DOM parser how i could use it – Vikram Anand Bhushan Mar 12 '15 at 06:48
  • 1
    Since they're formatted like XML/HTML tags, you can use a DOM parser to parse them. – Barmar Mar 12 '15 at 06:49
  • While the OP's string is very similar to XML I am seeing characters between the tags which I am assuming he means to preserve - I'm not sure if that can be done with a DOM parser... – Peter Bowers Mar 12 '15 at 07:11
  • @PeterBowers of course you can, they will be in text nodes. – ThW Mar 12 '15 at 11:10
  • @ThW, thanks - I've never seen the #text nodes before. I just tried to use them in http://www.w3schools.com/dom/tryit.asp?filename=try_dom_parsertest3 by putting a few words between the end of one tag and the start of another and it caused everything to fail. Which DOM parsers would support this better? – Peter Bowers Mar 12 '15 at 11:35
  • The parser supports it. But `x[i].childNodes[0].nodeValue` does not work on text nodes. They have no children obviously. Try `x[i].textContent`. Nodes have a nodeType property and different "classes", too. Usually you don't use the DOM functions to fetch nodes/data but XPath *btw*. – ThW Mar 12 '15 at 12:50
  • @ThW I tried parsing one of my string to the link provided by mister Peters but it result only #text as an output – Vikram Anand Bhushan Mar 12 '15 at 13:21

1 Answers1

0

You want to make a regex which will identify the surrounding tags and pull out the contents in between. Then, call a function which decides what that particular tag combination should be replaced with. preg_replace_callback seems to be the function du jour for you.

$newstr = '';
while ($newstr != $subject) {
        if ($newstr)
            $subject = $newstr;
        echo "subject=".str_replace('<', '&lt;', $subject)."<br />\n";
        $newstr = preg_replace_callback('/<(fld|fn|ltr)>([^>]*?)<\/\\1>/', 
            function ($matches) {
                // $matches[1] contains 'fld' or 'fn' or 'ltr'
                // $matches[2] contains the string between the tags
                echo "matches=<pre>".print_r($matches,true)."</pre><br />\n";
                switch ($matches[1]) {
                case 'fld':
                    // here you look up the string in $matches[2] from your XML and return it
                    return "fld:$matches[2]";
                    break;
                case 'fn':
                    // here you call whatever function with arg of $matches[2] and return result
                    return "fn:$matches[2]";
                    break;
                case 'ltr':
                    return "ltr:$matches[2]";
                    return $matches[2];
                    break;
                }
            }, $subject);
        echo "anewstr=".str_replace('<', '&lt;', $newstr)."<br />\n";
        if ($newstr == $subject) {
            echo "Breaking...<br />\n";
            break;
        }
}

Technically since you are returning a value in each case you wouldn't need the break; but I think it's good form to leave it in there.

Peter Bowers
  • 3,063
  • 1
  • 10
  • 18
  • The `` is going to be your challenge since it apparently allows tags to be nested. You may want to change my `(.*?)` to be `([^<]*)` to make sure you handle all inner tags first - however if you have any tags besides fn/fld/ltr inside the `...` then it will fail. – Peter Bowers Mar 12 '15 at 07:21
  • LOL, @ThW! But I wasn't 100% certain - were you saying we should or shouldn't parse HTML with regexes...? :-) – Peter Bowers Mar 12 '15 at 11:39
  • @PeterBowers can you please epxlain me how I can pass the string to the function that you mentioned in the answer. – Vikram Anand Bhushan Mar 12 '15 at 12:21
  • @PeterBowers when I tried to parse the 1st string its showing following error. `preg_replace_callback() [function.preg-replace-callback]: Unknown modifier '\' on line 18` – Vikram Anand Bhushan Mar 12 '15 at 13:34
  • $subject is the string you pass in. I was using an older reference style which caused the error - note $1 instead of \1. – Peter Bowers Mar 12 '15 at 17:22
  • 1
    OK, I actually tried it this time and it's now working. However, it still isn't going to handle nested tags. For that you would have to call it multiple times as long as the returned value was changing the string - I *think* that would solve it. However, based on other comments above I don't think I'm going to keep developing the regex-based solution... – Peter Bowers Mar 13 '15 at 05:51
  • Added the looping and it now handles some limited nested tags. Make sure you don't have a `>` in your data apart from the code. (The `<` in your 3rd example messed with my mind for a while.) – Peter Bowers Mar 13 '15 at 06:34