开发者

Indexed substitution within string

开发者 https://www.devze.com 2023-03-07 20:15 出处:网络
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

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_repl开发者_如何学Cace('{' . $matches[1][0] . '}', $matches[2][0], $output);

    print '<hr />';

    print '<pre>';
    print_r($matches);
    print '</pre>';


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;


<?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.

0

精彩评论

暂无评论...
验证码 换一张
取 消