PHP preg_split没有返回正确的值

时间:2017-09-08 14:23:44

标签: php wordpress

我试图在找到角色之后拆分一个wordpress Blogpost标题所以它不像爆炸那样切断,但它给了我以下var_dump:

array(2) { [0]=> string(0) "" [1]=> string(0) "" }

这是我的代码:

$title = $post['post_title'];
$titlepart = preg_split("/(.+)([.,?!]{1})(.+)/", $title);
var_dump($titlepart);

任何想法?

1 个答案:

答案 0 :(得分:0)

因此,我对自己要执行的操作的理解是,将title用某种控制字符分开,在该示例中,您选择了四个:,.!?。您希望标题会被分成两部分(因此控制字符肯定会出现,并且希望只会出现一次-尽管我们可以避免倍数,但我们必须做个假设-我将它们全部放在第二行)。

这里有两种解决方案-使用preg_matchstrpbrk。我在顶部($ts)包含了一些示例字符串,并在下面的评论中提供了预期的解决方案。

<?php

$ts=[
    'Koningsdag 2017: Waar komt het vandaan? En wat u moet weten over deze dag',
    'King\'s Day 2017: You love it! You won\'t believe how old it is',
    'Queen of hearts: Foul-tempered. Find out what Alice did',
    'Wise owl: What goes up, must come down',
    'Shakespeare: Some are born great, some achieve greatness, and some have greatness thrust upon them',
    //'No delimiter',
    //''
];
/* Desired outputs:(?)

    Koningsdag 2017: Waar komt het vandaan?
    En wat u moet weten over deze dag

    King's Day 2017: You love it!
    You won't believe how old it is

    Queen of hearts: Foul-tempered.
    Find out what Alice did

    Wise owl: What goes up,
    must come down

    Shakespeare: Some are born great,
    some achieve greatness, and some have greatness thrust upon them
*/

function report($res) {
    if (count($res)!==2) {throw new Exception("Unexpected result");}
    echo $res[0]."\n".$res[1]."\n\n";
}

function splitByPreg($title,$chars='.,?!') {
    //Split by $chars, capture the split marker so we can append it back to the first match
    // - If we see more than one $char we assume all further examples should be in part 2
    $parts=preg_split('/(['.preg_quote($chars,'/').'])\s*/', $title,2,PREG_SPLIT_DELIM_CAPTURE);
    $n=count($parts);
    switch ($n) {
        case 3:
            //Expected
            return [$parts[0].$parts[1],$parts[2]];
        case 2:
            //Not sure how this could happen
            return $parts;
        case 1:
            //No delim found
            return [$parts[0],""];
    }
    return ["",""];
}

function splitByChar($title,$chars='.,?!') {
    //This returns the second line as a string starting with the break character
    $lineTwo=strpbrk($title,$chars);
    if ($lineTwo===false) return [$title,""];
    $n=strlen($title);
    $break=$n-strlen($lineTwo)+1;//+1 to move the break character to line1
    return [substr($title,0,$break),trim(substr($title,$break))];
}

foreach ($ts as $t) {
    //Pick one:
    $res=splitByPreg($t);
    //$res=splitByChar($t);
    report($res);
}

如您所见,您可以为这两种方法指定分隔符(如果您希望对原始四种方法使用大于/替代)。我已经对其中的一些进行了测试,其中包括/(这有时仅是正则表达式中的控制字符。