Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 字符串内的索引替换_Php_String - Fatal编程技术网

Php 字符串内的索引替换

Php 字符串内的索引替换,php,string,Php,String,我希望转换一个带有特殊HTML标记的字符串,并相应地对其进行解析。下面我将展示原始字符串后面是我希望解析后的字符串。如果有人能告诉我一个合适的编码方法,使这成为可能,那将是非常棒的 原始字符串: $string = '<string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>'; 编辑: 我忘了添加$string变量可能有多个带多个选项的字符串,例如$string变量可以是以下内容:

我希望转换一个带有特殊HTML标记的字符串,并相应地对其进行解析。下面我将展示原始字符串后面是我希望解析后的字符串。如果有人能告诉我一个合适的编码方法,使这成为可能,那将是非常棒的

原始字符串:

$string = '<string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>';
编辑:

我忘了添加$string变量可能有多个带多个选项的字符串,例如$string变量可以是以下内容:

$string = '<string 1="hot dog">I like to have {1}</string> on <string 1="beach" 2="sun">the {1} with the blazing hot {2} staring down at me.';
$string='我喜欢把{1}放在{1}上,让炽热的{2}盯着我';
我需要一个可以解析上述代码示例的解决方案

编辑2:

下面是我开发的一个示例代码,它不完整并且有一些bug。如果有多个选项e.x.1='blah'2='blahblah',则不会解析第二个选项

$string = '<phrase 1="Jacob" 2="cool">{1} is {2}</phrase> when <phrase 1="John" 2="Chris">{1} and {2} are around.</phrase>';

preg_match_all('/<phrase ([0-9])="(.*?)">(.*?)<\/phrase>/', $string, $matches);

    print $matches[1][0] . '<br />';
    print $matches[2][0] . '<br />';
    print $matches[3][0] . '<br />';

    print '<hr />';

    $string = $matches[3][0];

    print str_replace('{' . $matches[1][0] . '}', $matches[2][0], $output);

    print '<hr />';

    print '<pre>';
    print_r($matches);
    print '</pre>';
$string='{1}在{1}和{2}附近时,{1}是{2}';
preg_match_all(“/(.*?/”,$string,$matches);
打印$matches[1][0]。'
; 打印$matches[2][0]。'
; 打印$matches[3][0]。'
; 打印“
”; $string=$matches[3][0]; 打印str_replace('{.$matches[1][0].}',$matches[2][0],$output); 打印“
”; 打印“”; 打印(匹配项); 打印“”;

这是一个不使用正则表达式的版本,看起来更直截了当。我不知道xml解析器如何处理以数字开头的属性。



这是一个不使用正则表达式的版本,看起来更直截了当。我不知道xml解析器如何处理以数字开头的属性。

由于
$string
不是有效的xml(例如,包含数字作为属性名称),您可以尝试:

$string2 = '<string 1="hot dog">I like to have {1}</string> on <string 1="beach" 2="sun">the {1} with the blazing hot {2} staring down at me.</string>';
$parsed_string2 = '';
$a = explode('</string>', $string2);
foreach ($a as $s) {
    $parsed_elm = strip_tags($s);
    for ($i = 1; $i <= 2; $i++) {
        if (preg_match('/' . $i . '="([^"]+)"/', $s, $match))
            $parsed_elm = str_replace('{' . $i .'}', $match[1], $parsed_elm);
    }
    $parsed_string2 .= $parsed_elm;
}
echo $parsed_string2;
$string='{1}喜欢有很多{2}';
$parsed_string=strip_标记($string);

对于($i=1;$iAs
$string
不是有效的XML(例如,包含数字作为属性名),您可以尝试:

$string2 = '<string 1="hot dog">I like to have {1}</string> on <string 1="beach" 2="sun">the {1} with the blazing hot {2} staring down at me.</string>';
$parsed_string2 = '';
$a = explode('</string>', $string2);
foreach ($a as $s) {
    $parsed_elm = strip_tags($s);
    for ($i = 1; $i <= 2; $i++) {
        if (preg_match('/' . $i . '="([^"]+)"/', $s, $match))
            $parsed_elm = str_replace('{' . $i .'}', $match[1], $parsed_elm);
    }
    $parsed_string2 .= $parsed_elm;
}
echo $parsed_string2;
$string='{1}喜欢有很多{2}';
$parsed_string=strip_标记($string);

对于($i=1;$i我将把它解析为XML,然后编写代码用regexplore进行替换。请注意,尽管原始字符串看起来像XML,但如果属性以数字开头,则认为XML无效。Jacob,您需要以数字开头的属性吗?我将把它解析为XML,然后编写代码来进行替换RegExplase的替换请注意,虽然原始字符串看起来像xml,但如果属性以数字开头,则认为xml无效。Jacob,您需要以数字开头的属性吗?谢谢,这正是我要找的。如果您有时间,可以解释代码的每一步吗?谢谢,th这正是我想要的。如果你有时间,你能解释一下代码的每一步吗?
$string = '<string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>';
$parsed_string = strip_tags($string);
for ($i = 1; $i <= 2; $i++) {
    if (preg_match('/' . $i . '="([^"]+)"/', $string, $match))
        $parsed_string = str_replace('{' . $i .'}', $match[1], $parsed_string);
}
echo $parsed_string;
$string2 = '<string 1="hot dog">I like to have {1}</string> on <string 1="beach" 2="sun">the {1} with the blazing hot {2} staring down at me.</string>';
$parsed_string2 = '';
$a = explode('</string>', $string2);
foreach ($a as $s) {
    $parsed_elm = strip_tags($s);
    for ($i = 1; $i <= 2; $i++) {
        if (preg_match('/' . $i . '="([^"]+)"/', $s, $match))
            $parsed_elm = str_replace('{' . $i .'}', $match[1], $parsed_elm);
    }
    $parsed_string2 .= $parsed_elm;
}
echo $parsed_string2;