拆分并合并内容

时间:2014-09-05 02:27:46

标签: php design-patterns extract

我有内容。

{this is title1|this is title2}
{this is para11|this is para21}
{this is para12|this is para22}
{this is para13|this is para23}

我想把它们分成两个内容。

内容1:

this is title
this is para11
this is para12
this is para13

内容2:

this is title
this is para21
this is para22
this is para23

我至少试过在数组中获取第1个内容,但它不会出现。

function findinside($start, $end, $string) {
    preg_match_all('/' . preg_quote($start, '/') . '([^\.)]+)'. preg_quote($end, '/').'/i', $string, $m);
    return $m[1];
}
$content = "{this is title1|this is title2}

{this is para11|this is para21}

{this is para12|this is para22}

{this is para13|this is para23}";
$start_string = '{';
$stop_string = '|';
$strings  = findinside($start_string,$stop_string,$content);
echo '<pre>';
print_r($strings);
echo '</pre>';

输出:

Array
(
 [0] => this is title1|this is title2}

 {this is para11|this is para21}

 {this is para12|this is para22}

{this is para13
)

有人可以为2个内容提供帮助。如果它将超过2个内容。我现在正在寻找2个内容。但是,如果它适用于理想的n个内容。感谢

4 个答案:

答案 0 :(得分:1)

这应该有效:

<?php
$content = "{this is title1|this is title2}

{this is para11|this is para21}

{this is para12|this is
para22}

{this is para13|this is para23}{this is para14|this is para24}


{this is 
para14|this is para24}";
#$c = array_values(array_filter(explode(PHP_EOL, $content)));
#$c = explode("}{", str_replace(array("}\r", "}\n"), "}", substr($content, 1, -1)));
$c = preg_split("#}\s*{#", substr($content, 1, -1));
$a = array();
foreach($c as $cc){
    $t = explode('|', $cc);
    foreach($t as $k => $v){
        $a[$k][] = $t[$k];
    }
}
var_dump($a[0], $a[1]);#depending on how many items access them by $a[n]

答案 1 :(得分:0)

你的角色类必须排除$end,因为量词是贪婪的,它会尝试一直匹配到最后一个结尾的角色,而不是第一个。

function findinside($start, $end, $string) {
    preg_match_all('/' . preg_quote($start, '/') . '([^\.)' . preg_quote($end) . ']+)'. preg_quote($end, '/').'/i', $string, $m);
    return $m[1];
}

答案 2 :(得分:0)

试试这个:

$res = preg_split('/\||\}.*\{/Us', substr($content, 1, -1));
for($i=0,$n=1,$l=count($res); $n<$l; $i+=2,$n+=2){
  $content_1[] = $res[$i]; $content_2[] = $res[$n];
}

答案 3 :(得分:0)

function findinside($start, $end, $middle, $string) {
    $prepare = array();
    $result = array('content_1' => array(), 'content_2' => array());

    $rc = (bool)preg_match_all("/".$start."(.*?)".$end."/", $string, $prepare);
    if($rc){
        foreach ($prepare[1] as $str){
            list($c1, $c2) = explode($middle, $str);
            $result['content_1'][] = $c1;
            $result['content_2'][] = $c2;
        }
    }

    return $result;
}

调用:

$strings = findinside('{', '}', '|', $content);

echo '<pre>';
print_r($strings);
echo '</pre>';

输出:

Array
(
[content_1] => Array
    (
        [0] => this is title1
        [1] => this is para11
        [2] => this is para12
        [3] => this is para13
    )
[content_2] => Array
    (
        [0] => this is title2
        [1] => this is para21
        [2] => this is para22
        [3] => this is para23
    )
)