字符串内的索引替换

发布于 2024-11-08 15:32:41 字数 1347 浏览 0 评论 0原文

我希望使用特殊的 HTML 标签转换字符串并相应地解析它。下面我将展示原始字符串后面跟着我想要解析的字符串。如果有人可以指导我采用正确的编码方法来实现这一点,那就太棒了。

原始字符串:

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

解析的字符串:

$parsed_string = 'Jacob likes to have a lot of ice cream.';]

编辑:

我忘记添加 $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.';

我需要一个可以解析上面的代码示例的解决方案。

编辑2:

这是我开发的示例代码,它不完整并且有一些错误。如果有多个选项,例如 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>';

I am looking to convert a string with a special HTML tag and parse it accordingly. Below I will show what the original string is followed by what I want the parsed string to be. If someone can direct me towards a proper coding method to make this possible that would be fantastic.

Original String:

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

Parsed String:

$parsed_string = 'Jacob likes to have a lot of ice cream.';]

EDIT:

I forgot to add that the $string variable may having multiple strings with multiple options, for example the $string variable could be the following:

$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.';

I need a solution that can parse the code example above.

EDIT 2:

Here is a sample code I developed that is incomplete and has a few bugs. If there is more than one option e.x. 1='blah' 2='blahblah' it will not parse the second option.

$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>';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-11-15 15:32:41

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

$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;

UPDATE

您的编辑从一个 标签现在在变量中有多个 标签。这个应该适用于多个:

$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;

As $string is no valid XML (e.g. containing numbers as attribute names), you may try:

$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;

UPDATE

Your EDIT switched from having one <string> tag to having multiple <string> tags in the variable now. This one should work for multiples:

$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;
白云不回头 2024-11-15 15:32:41
<?php
    $rows = array();
    $xml = "
        <string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>        
        <string 1="John" 2="cream">{1} likes to have a lot of {2}.</string>     
    "
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, trim($xml), $xml_values);        
    foreach ($xml_values as $row){
        $finalRow = $row['values'];
        foreach ($row['attributes'] as $att => $attval){
            $finalRow = str_replace ($finalRow, "{".$att."}", $attval);
        }
        $rows[] = $finalRow;
    }
?>

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

<?php
    $rows = array();
    $xml = "
        <string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>        
        <string 1="John" 2="cream">{1} likes to have a lot of {2}.</string>     
    "
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, trim($xml), $xml_values);        
    foreach ($xml_values as $row){
        $finalRow = $row['values'];
        foreach ($row['attributes'] as $att => $attval){
            $finalRow = str_replace ($finalRow, "{".$att."}", $attval);
        }
        $rows[] = $finalRow;
    }
?>

Here's a version that doesn't use regex, this seemed more straight forward. I don't know how the xml parser would cope with attributes that start with a number though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文