开发者

Add a word to the ending of every third sentence

开发者 https://www.devze.com 2022-12-16 09:55 出处:网络
I\'m creating a \"fun-translator\", and I\'m trying to add a word to the end of every third sentence or so.

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

精彩评论

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

关注公众号