-1

I have a string like below:

For Example:

$string = "A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a replica of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list.";

Now I need to get the content up to 3 line and it should be a complete sentence. So I want to know how can I do it. Is there any function in PHP to do it?

sth
  • 222,467
  • 53
  • 283
  • 367
Toretto
  • 4,721
  • 5
  • 27
  • 46
  • Do you mean the third character? All of that text is on one line, I see no new line breaks `\n`. – EM-Creations Feb 20 '13 at 10:11
  • 4
    There is only one line in your input? Also, how would you recognize a sentence - what if there's a `.` within a sentence like so: `I earned $1,000,000,000.00 this year!` – Pekka Feb 20 '13 at 10:11
  • 1
    Do you mean 3 line means 3 fullstop?? – 웃웃웃웃웃 Feb 20 '13 at 10:13
  • You want to split your string into 3 lines and not let one sentence carry over to the next line? You have 4 sentences, what happens with the last one? – silkfire Feb 20 '13 at 10:13
  • It's a paragraph with 3 to 4 sentences. I just want to fetch the content up to 3 rd complete sentence. – Toretto Feb 20 '13 at 10:14
  • @user yes that's what i want to fetch 3 completed sentence of given string. – Toretto Feb 20 '13 at 10:15

2 Answers2

0

This should start you off with some basic regular expressions. You should keep re-testing the validness of the Regex and tweak it when you encounter exceptional cases. You can't realy guard against anything (like silkfire stated in the comment on your question).

<?php
$string = "A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a replica of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list.";

$result = preg_match_all("/(?:\w+\s)+(?:\w+\.\s|\w+\.[a-z](?:\w+\s)+(?:\w+\.)+)/", $string, $matches);

echo '<pre>';
print_r($matches);
echo '</pre>';
?>

The problem is with your String that you never really know what "lastword.Wordofnewline" is in code-logic. The regex above is actually a same inside itself. This will only match one of the cases tho, like this string where replica becomes repli.ca:

A great way to start with your theme is to use the XML export file from the demo. Having this starting content makes it easier to see how some features are created. Download the demo content below and import it into your WordPress site to get a repli.ca of the theme demo site.The second step is to navigate to the Tools options and click Import. Then on the bottom of the list.

You really have to customize the regex based on a lot of testing before you can get it to work. If possible (and if that is the case) you should let your users (the writers) validate the output of your code's automatic edits :).

  • it's not working because whenever `.` occur in the string it break in to part. – Toretto Feb 20 '13 at 10:50
  • I've updated the question. It's not that easy, as there is no "logic" in language structure when people don't always write it in conformity :). If the new line start with a lower case and is written against the dot you are probably even more in a bind with these regex checks :P –  Feb 20 '13 at 12:10
0

You need your string formatted so that it either has /n or . after each sentence, then you can split the string (php explode function). Anything weird like £49.99 or a 'Dr. john Smith' will break the algorithm.

Husman
  • 6,819
  • 9
  • 29
  • 47
  • Take a casus where you fetch a block of text with cURL from a website (span-block's innerHTML for example). The text has no newline-breaks, but you still want to figure out the individual lines. Your answer focuses on adjusting the source, but we don't know if he has control of that source string's format. If that 'is' the case; I guess then he can go over to adjusting his pre-code to make the fetching off the lines afterwards easier :) –  Feb 20 '13 at 12:18