我想将一些文本解析成数组:
我的文字如下:
You've come to the {right; correct; appropriate} place! Start by {searching; probing; inquiring} our site below, or {browse; {search; lookup; examine}} our list of popular support articles.
第三组单词具有嵌套标签。如何忽略打开和关闭嵌套标记以实现诸如
之类的数组$tags[0][0] = 'right';
$tags[0][1] = 'suitable';
$tags[0][2] = 'appropriate';
$tags[1][0] = 'searching';
$tags[1][1] = 'probing';
$tags[1][2] = 'inquiring';
$tags[2][1] = 'browse';
$tags[2][2] = 'search';
$tags[2][3] = 'lookup';
$tags[2][4] = 'examine';
基本上忽略了标签的嵌套。 任何帮助将不胜感激。
我目前唯一的想法就是逐字逐句地遍历文本,直到找到{会增加“深度”变量。捕获两者之间的单词,直到我找到}减少深度变量并且在它返回到零时,停止捕获单词。我只是想知道是否有更简单的方法来做到这一点。感谢。
感谢您的出色帮助,我对其进行了一些修改,以便提出以下解决方案。
$code = "You've come to {the right; the correct; the appropriate} place!
Start by {searching; probing; inquiring} our site below, or
{browse; {search; {foo; bar}; lookup}; examine} our list of
popular support articles.";
echo $code."\r\n\r\n";
preg_match_all('/{((?:[^{}]*|(?R))*)}/', $code, $matches);
$arr = array();
$r = array('{','}');
foreach($matches[1] as $k1 => $m)
{
$ths = explode(';',str_replace($r,'',$m));
foreach($ths as $key => $val)
{
if($val!='')
$arr[$k1][$key] = trim($val);
$code = str_replace($matches[0][$k1],'[[rep'.$k1.']]',$code);
}
}
echo $code;
返回
你来{正确;正确的;适当的地方!从{搜寻;探测;在我们的网站下面查询,或{浏览; {搜索; {FOO;酒吧};抬头};检查我们的热门支持文章列表。
你来[[rep0]]的地方!从下面的[[rep1]]我们的网站开始,或者[[rep2]]我们的热门支持文章列表。
答案 0 :(得分:1)
我目前唯一的想法就是逐字逐句地遍历文本,直到找到{会增加“深度”变量。捕获两者之间的单词,直到我找到}减少深度变量并且在它返回到零时,停止捕获单词。我只是想知道是否有更简单的方法来做到这一点。
这听起来像是一种合理的方式。另一种方法是使用一些正则表达式,尽管可能会导致一个解决方案(远远)可读性较差(因此可维护性较差)。
<?php
$text = "You've come to the {right; correct; appropriate} place!
Start by {searching; probing; inquiring} our site below, or
{browse; {search; {foo; bar}; lookup}; examine} our list of
popular support articles. {the right; the correct; the appropriate}";
preg_match_all('/{((?:[^{}]*|(?R))*)}/', $text, $matches);
$arr = array();
foreach($matches[1] as $m) {
preg_match_all('/\w([\w\s]*\w)?/', $m, $words);
$arr[] = $words[0];
}
print_r($arr);
?>
会产生:
Array
(
[0] => Array
(
[0] => right
[1] => correct
[2] => appropriate
)
[1] => Array
(
[0] => searching
[1] => probing
[2] => inquiring
)
[2] => Array
(
[0] => browse
[1] => search
[2] => foo
[3] => bar
[4] => lookup
[5] => examine
)
[3] => Array
(
[0] => the right
[1] => the correct
[2] => the appropriate
)
)