0

I would like to add span tag for every sentence which ended with "." like: my string:

"I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal. The whole message of the crucifixion was simply that I did not."

O/P:

"<span id="s1">I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal.</span> <span id="s2">The whole message of the crucifixion was simply that I did not.</span>"

how it possible with php?

Wiram Rathod
  • 1,895
  • 1
  • 19
  • 41
  • Yes? Yes?! Yes! Yay! - Which specific problem do you run into, where do you hit the roadblock? *"how it possible with php?"* -> with programming! But it depends on your concrete needs how this is done, normally this is either a string operation, a DOM operation or both! Also you might not want to look like a help vampire, so educate about them: http://slash7.com/2006/12/22/vampires/ – hakre Jun 10 '13 at 06:56
  • Please define "Sentence" in your question. As far as you've told everything is a sentence that ends with a dot - even the dot alone. Wihtout a clean specification, the word "Sentence" should not be used in a programming question unless you've made clear which natural language toolkit you're using that defines such a term very clearly. And as this is about natural language, pure PHP code is not really the best one to solve such problems. It's not well stuffed for natural language analysis. – hakre Jun 10 '13 at 07:00

4 Answers4

1

You could do

<?php

$string="I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal. The whole message of the crucifixion was simply that I did not.";
$output=str_replace(". ",'. </span> <span id="s2">',$string);
echo '<span id="s1">'.$output.'</span>';
?>

Edit Based on Comments

This version will make sure every new replacement gets a new span id

<?php
$string="I could not have said, ’Betrayest thou the Son of man with a kiss?’ unless I believed in betrayal. The whole message of the crucifixion was simply that I did not. Testing 123. testing again.";
$dots=substr_count($string,". ");
for($i=2;$i<=$dots+2;$i++)
{
$string=preg_replace("/\. /", ".</span> <span id =\"s$i\">" ,$string,1);
}
echo '<span id="s1">'.$string.'</span>';
?>
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

If you explode the sentence with ". ". I want to modify the code above.

$newText = "";
$count = 0;
foreach (explode(". ",$theText) as $part) {

   if(ctype_upper($part{0}))
   {
      $count++;
      $newText .= "<span id=\"s$count\">$part</span>";
  }

 }

I hope it should work for abbreviations or something.

Amar Banerjee
  • 4,992
  • 5
  • 34
  • 51
  • If the first letter is not upper case you should at least still add the part as it is, you're just throwing the part away, so this won't work. – MaX Jun 10 '13 at 06:59
  • Also, this won't catch cases where there's a newline after the period and not a space. Who's upvoting these incorrect solutions? – MaX Jun 10 '13 at 07:05
0

Try this code, You will get unique id for span tag.

<?php
$str = "I could not have said, 'Betrayest thou the Son of man with a kiss?' unless I believed in betrayal. The whole message of the crucifixion was simply that I did not.";
$exp_str = explode(". ",$str);
$sentence = "<span id=\"s1\">";

$n = 2;
foreach($exp_str as $val) {
    $sentence .= $val.".</span> <span id=\"s$n\">";
    $n++;
}
$n = $n-1;
$sentence = substr($sentence,0,strpos($sentence,".</span> <span id=\"s$n\">"));
echo $sentence;
?>
6339
  • 475
  • 3
  • 16
-2

I'm not sure if preg_replace can do this (because of the unique IDs). Otherwise it can be manually done like this:

$newText = "";
$count = 0;
foreach (explode(".",$theText) as $part) {
  $count++;
  $newText .= "<span id=\"s$count\">$part</span>";
}

But what about periods mid sentence, as in abbreviations or something? You could probably replace the explode with a preg_split to get a better result. For instance only split if the char right after the period is not a letter or another period.

preg_split("/\.[^\,\w]/",$theText);

Or just ensure the next char is a whitespace.

preg_split("/\.\s/",$theText);
MaX
  • 1,765
  • 13
  • 17
  • It's good practice to leave a comment explaining why you downvote. Otherwise, how will I learn? I'm puzzled how mine gets downvoted and two wrong/incomplete answers are upvoted. – MaX Jun 10 '13 at 09:10