PHP 中复杂的字符串操作 [英] complicated string manipulation in PHP

查看:25
本文介绍了PHP 中复杂的字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的表格.

 
<table width="70%" cellpadding="2" border="1" style="margin: 0 auto;margin-bottom:70px" class="parametros"><标题><h1>Parámetros generales de la plantilla</h1></caption><tr><th colspan="2">Campo</th><th colspan="6">Filtro</th></tr><tr class="row"><td class="dbList" colspan="2">地址</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></选择><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></表单>

所以 select 中的选项实际上是来自 XML 的列.具有相应字段的数据

给你一个想法,xml看起来像这样

<阿努西奥><CIUDAD>一些名字</CIUDAD><MUNICIPIO>某些名称</MUNICIPIO><ESTADO>一些名字</ESTADO></ANUNCIO><阿努西奥><CIUDAD>一些名字</CIUDAD><MUNICIPIO>某些名称</MUNICIPIO><ESTADO>一些名字</ESTADO></ANUNCIO><盐测试>

用户将使用选择选项并创建一个字符串,该字符串将具有创建内容的设置

例如

要创建地址字符串,用户将保存类似这样的设置.

字符串 1 :

SALTEST.ANUNCIO.ESTADO SALTEST.ANUNCIO.MUNICIPIO SALTEST.ANUNCIO.CIUDAD

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)

所以基本上字符串会有三种类型的分隔符.

1.这些是要从 XML 使用的字段.

2.这些之间的任何东西都是一个函数,我告诉系统将它检测为一个函数

3.这些之间的任何东西都是必须用来分隔每个

的字符

例如:

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

应该给一些名字,一些名字,一些名字.

我必须编写一个PHP脚本来检测每种类型的字符串,然后应用相关功能

解决方案

您想要制作一个正则表达式,用于识别周围的标签并提取其间的内容.然后,调用一个函数来决定应该用什么来替换特定的标签组合.preg_replace_callback 似乎是您du jour 的功能.

$newstr = '';而 ($newstr != $subject) {如果 ($newstr)$subject = $newstr;echo "subject=".str_replace('<', '&lt;', $subject)."<br/>\n";$newstr = preg_replace_callback('/<(fld|fn|ltr)>([^>]*?)<\/\\1>/',功能($匹配){//$matches[1] 包含 'fld' 或 'fn' 或 'ltr'//$matches[2] 包含标签之间的字符串echo "matches=

".print_r($matches,true)."</pre><br/>\n";开关($matches[1]){案例'fld'://在这里,您从 XML 中查找 $matches[2] 中的字符串并返回它返回 "fld:$matches[2]";休息;案例'fn'://在这里你用 $matches[2] 的参数调用任何函数并返回结果返回 "fn:$matches[2]";休息;案例'ltr':返回 "ltr:$matches[2]";返回 $matches[2];休息;}}, $subject);echo "anewstr=".str_replace('<', '&lt;', $newstr)."<br/>\n";如果($newstr == $subject){echo "破坏...<br/>\n";休息;}}

从技术上讲,由于您在每种情况下都返回一个值,因此您不需要 break; 但我认为将它留在那里是一种很好的形式.

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

解决方案

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.

这篇关于PHP 中复杂的字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆