tags:

views:

139

answers:

1

Hello 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 then find a puncation, but I really don't know regular expression to well.

Anyone knows how I can get it to work?

+1  A: 

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 sohuld do it

marvin
yeah this works great
streetparade
If you know you only need every third item in the array, why bother iterating over it one by one? for($i = 2; $i < $numberOfSentences; $i += 3)
bish
This one would add " some fun strin" to the end of urls etc .. And that won't work :/
Terw