webmasters
webmasters

Reputation: 5831

Slice sentences in a text and storing them in variables

I have some text inside $content var, like this:

$content = $page_data->post_content;

I need to slice the content somehow and extract the sentences, inserting each one inside it's own var. Something like this:

$sentence1 = 'first sentence of the text';
$sentence2 = 'second sentence of the text';

and so on...

How can I do this?

PS I am thinking of something like this, but I need somekind of loop for each sentence:

$match = null;
preg_match('/(.*?[?\.!]{1,3})/', $content, $match);  
$sentence1 = $match[1];   
$sentence2 = $match[2]; 

Ty:)

Upvotes: 0

Views: 211

Answers (4)

connec
connec

Reputation: 7421

Assuming a sentence is delimited by terminating punctuation, optionally followed by a space, you can do the following to get the sentences in an array.

$sentences = preg_split('/[!?\.]\s?/', $content);

You may want to trim any additional spaces as well with

$sentences = array_map('trim', $sentences);

This way, $sentences[0] is the first, $sentences[1] is the second and so on. If you need to loop through them you can use foreach:

foreach($sentences as $sentence) {
  // Do something with $sentence...
}

Upvotes: 0

Hammerite
Hammerite

Reputation: 22340

Don't use individually named variables like $sentence1, $sentence2 etc. Use an array.

$sentences = explode('.', $page_data->post_content);

This gives you an array of the "sentences" in the variable $page_data->post_content, where "sentences" really means sequences of characters between full stops. This logic will get tripped up wherever a full stop is used to mean something other than the end of a sentence (e.g. "Mr. Watson").

Edit: Of course, you can use more sophisticated logic to detect sentence boundaries, as you have suggested. You should still use an array, not create an unknown number of variables with numbers on the ends of their names.

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382736

Assuming each sentence ends with full stop, you can use explode:

$content = $page_data->post_content;
$sentences = explode('.', $content);

Now your sentences can be accessed like:

echo $sentences[0];    // 1st sentence
echo $sentences[1];    // 2nd sentence
echo $sentences[2];    // 3rd sentence
// and so on

Note that you can count total sentences using count or sizeof:

echo count($sentences);

It is not a good idea to create a new variable for each sentence, imagine you might have long piece of text which would require to create that number of variables there by increasing memory usage. You can simply use array index $sentences[0], $sentences[1] and so on.

Upvotes: 1

Sawny
Sawny

Reputation: 1423

Do you need them in variables? Can't you use a array?

$sentence = explode(". ", $page_data->post_content);

EDIT:

If you need variables:

$allSentence = explode(". ", $page_data->post_content);
foreach($allSentence as $key => $val)
{
    ${"sentence". $key} = $val;
}

Upvotes: 2

Related Questions