字符串内的索引替换
我希望使用特殊的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
$string
不是有效的 XML(例如,包含数字作为属性名称),您可以尝试:UPDATE
您的编辑从一个标签现在在变量中有多个
标签。这个应该适用于多个:As
$string
is no valid XML (e.g. containing numbers as attribute names), you may try:UPDATE
Your EDIT switched from having one
<string>
tag to having multiple<string>
tags in the variable now. This one should work for multiples:这是一个不使用正则表达式的版本,这看起来更直接。我不知道 xml 解析器如何处理以数字开头的属性。
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.