开发者

I'm trying to parse some text in html with custom nested tags

I would like to parse some text into an array:

My text looks like this:

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.

The third group of words has nested tags. How can I ignore the opening and closing nested tags to achieve an array such as

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

Essentially ignoring the nesting of the tags. Any help would be greatly appreciated.

My only current ideas for this is to traverse the text character by character until I find a { which would increment a "depth" variable. Capture the words in between until I find a } decreasing the depth variable and upon it returning to zero, stop capturing words. I was just wondering if there's a much easier way of doing this. Thanks.

Thanks for your excellent help, I modified it a bit to come up with the following solution.

$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;
开发者_Go百科

Returns

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.

You've come to [[rep0]] place! Start by [[rep1]] our site below, or [[rep2]] our list of popular support articles.


My only current ideas for this is to traverse the text character by character until I find a { which would increment a "depth" variable. Capture the words in between until I find a } decreasing the depth variable and upon it returning to zero, stop capturing words. I was just wondering if there's a much easier way of doing this.

That sounds like a reasonable way to do it. Another way to do this is by using a bit of regex, although that might result in a solution that is (far) less readable (and therefor less maintainable) than your own solution.

<?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);

?>

would produce:

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
        )

)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜