开发者

Add a word to the ending of every third sentence

I'm creating a "fun-translator", and I'm trying to add a word to the end of every third sentence or so.

It gets another page HTML code and translate it into teen language. But I want to add a word to every third sentence. I've been using this line for now:

$str = preg_replace_callback('{<.*?[^>]*>([æøåÆØÅ !,\w\d\-\(\)]+)([<|\s|!|\.|:])</.*?>}',
"assIt", $str);

But it does only add the word when the sentence is surrounded by HTML code.

I thougt that I could find every sentence by checking for a big letter and th开发者_如何转开发en find a puncation, but I really don't know regular expression to well.

Anyone knows how I can get it to work?


A little bit longer, but instead of regexp, you can use explode() function.

$sentences = explode('.', $str);
$numberOfSentences = count($sentences);
for($i = 0; $i < $numberOfSentences; $i++)
{
    if($i%3 == 2) {
        $sentences[$i] = $sentences[$i] . ' some fun string';
    }
}
echo implode('.', $sentences);

This should do it

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜