kevin_newbie Posted April 21, 2010 Share Posted April 21, 2010 Hello, I am working on a project and I want to explode after the second period of the description but if the sentence has a "Dr." I don't want that to be counted as part of the array. This is what I have now: $content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc Dr. Tom id lectus ante. Suspendisse potenti. Fusce eu ante mattis eros hendrerit imperdiet. Fusce at ante mauris, vel dapibus quam. Duis vel vestibulum neque. Aenean viverra condimentum ante, ut vulputate diam volutpat non. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh diam, sollicitudin at vulputate a, commodo nec ligula." $explode = explode('.', $content); echo $content[0] . '.' . $content[1] . '.' ; The code above displays on the browser as: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc Dr. I would like it to skip the "Dr." if possible. Is it? Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/ Share on other sites More sharing options...
premiso Posted April 21, 2010 Share Posted April 21, 2010 Using preg_split this should be possible. $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc Dr. Tom id lectus ante. Suspendisse potenti. Fusce eu ante mattis eros hendrerit imperdiet. Fusce at ante mauris, vel dapibus quam. Duis vel vestibulum neque. Aenean viverra condimentum ante, ut vulputate diam volutpat non. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh diam, sollicitudin at vulputate a, commodo nec ligula."; $array = preg_split("~[^D][^r]\.~", $string); print_r($array); Maybe not done the best it could be (or properly), but should work. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046034 Share on other sites More sharing options...
kevin_newbie Posted April 21, 2010 Author Share Posted April 21, 2010 Why does it remove the last letter of word before it puts a period? Also can I add in a "? and !" part? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046043 Share on other sites More sharing options...
Andy-H Posted April 21, 2010 Share Posted April 21, 2010 Does this work? $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc Dr. Tom id lectus ante. Suspendisse potenti. Fusce eu ante mattis eros hendrerit imperdiet. Fusce at ante mauris, vel dapibus quam. Duis vel vestibulum neque. Aenean viverra condimentum ante, ut vulputate diam volutpat non. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh diam, sollicitudin at vulputate a, commodo nec ligula."; $array = preg_split("~\b\.\b~", $string); print_r($array); Not too good with regular expressions but lookup word boundaries. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046051 Share on other sites More sharing options...
Ken2k7 Posted April 21, 2010 Share Posted April 21, 2010 Andy-H, I doubt \b can differentiate the dot between "blah blah blah. blah blah" and "Dr. Blah Blah[/tt]. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046055 Share on other sites More sharing options...
sug15 Posted April 21, 2010 Share Posted April 21, 2010 I actually have a thread asking a similar question: http://www.phpfreaks.com/forums/index.php/topic,295323.0.html Read that for some ideas. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046059 Share on other sites More sharing options...
kevin_newbie Posted April 21, 2010 Author Share Posted April 21, 2010 @ Andy-H - I did your code and I got this error "Notice: Undefined offset: 1 . . . " :/ @sug15 - so did you ended up using the split function in php? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046066 Share on other sites More sharing options...
cyberRobot Posted April 22, 2010 Share Posted April 22, 2010 Out of curiosity, is "Dr." the only thing you need to worry about? What about other things like: Mr. Mrs. The main navigation; which contains the About Us, Contact Us, etc. links; is on the left This is an interesting idea, but... John M. Smith Have you ever played .Hack? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046400 Share on other sites More sharing options...
TeddyKiller Posted April 22, 2010 Share Posted April 22, 2010 I'm not too sure whether it'd work.. but what about an explode? It depends on your system, but from what you gave its limitted. $take = 'Dr.'; $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc Dr. Tom id lectus ante. Suspendisse potenti. Fusce eu ante mattis eros hendrerit imperdiet. Fusce at ante mauris, vel dapibus quam. Duis vel vestibulum neque. Aenean viverra condimentum ante, ut vulputate diam volutpat non. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh diam, sollicitudin at vulputate a, commodo nec ligula."; $string = explode($take, $string); echo $string[0] . $string[1] Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046401 Share on other sites More sharing options...
dgoosens Posted April 22, 2010 Share Posted April 22, 2010 Out of curiosity, is "Dr." the only thing you need to worry about? What about other things like: Mr. Mrs. The main navigation; which contains the About Us, Contact Us, etc. links; is on the left This is an interesting idea, but... John M. Smith Have you ever played .Hack? that's the good question... but what are you trying to achieve ?? we might come up with alternate solutions... Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046403 Share on other sites More sharing options...
kevin_newbie Posted April 22, 2010 Author Share Posted April 22, 2010 Hello, I have an update, I found a function where it grabs the first leading or however many sentence you want from a string. So it finds the sentence that either has a "! . or ?" which are considered leading sentences. Now is there a way to find a match where if there are any "something." with in this function we can just skip over that? function getLeadingSentences($data, $max) { $re = "[^s*[^.?!]+[.?!]+s*]"; $out = ""; for($i = 0; $i < $max; $i++) { if(preg_match($re, $data, $match)) { //if a sentence is found, take it out of $data and add it to $out $out .= $match[0]; $data = preg_replace($re, "", $data); } else { $i = $max; } } return $out; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046480 Share on other sites More sharing options...
cyberRobot Posted April 22, 2010 Share Posted April 22, 2010 Now is there a way to find a match where if there are any "something." with in this function we can just skip over that? In my mind there are just too many options to catch everything. But maybe someone has a somewhat decent solution? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046484 Share on other sites More sharing options...
TeddyKiller Posted April 22, 2010 Share Posted April 22, 2010 but what are you trying to achieve ?? we might come up with alternate solutions... Question still stands Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046496 Share on other sites More sharing options...
MadTechie Posted April 22, 2010 Share Posted April 22, 2010 I think I had this problem a few weeks ago, I was trying to break a paragraph into sentences, is that what your trying to achieve ? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046526 Share on other sites More sharing options...
kevin_newbie Posted April 22, 2010 Author Share Posted April 22, 2010 Yeah the function is doing that but it breaks if there is something like "Mr." or "Dr." anything that is two letters with a "." in front of it. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046528 Share on other sites More sharing options...
litebearer Posted April 22, 2010 Share Posted April 22, 2010 Old man musing... $string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc Dr. Tom id lectus ante. Suspendisse potenti. Fusce eu ante mattis eros hendrerit imperdiet. Fusce at ante mauris, vel dapibus quam. Duis Mr. vel vestibulum neque. Aenean viverra condimentum ante, ut vulputate diam volutpat non. Cum Mrs. sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce nibh diam, sollicitudin at vulputate a, commodo nec ligula."; $remove_this = array(" Dr. ", " Ms. ", " Mr. ", " Mrs. "); $string = str_replace($remove_this, " ", $string); echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046540 Share on other sites More sharing options...
kevin_newbie Posted April 22, 2010 Author Share Posted April 22, 2010 I don't want to remove those I just want the function to skip over those so that it is not considered an end to a sentence. Right now I am doing a function that replace the word to full words. function replaceWord($description) { $abrev = array("dr.","dr", "DR.", "DR", "Dr.", "Dr" ); $words = array("doctor", "doctor", "Doctor", "Doctor", "Doctor", "Doctor"); $change = str_replace($abrev, $words, $description); return $change; } It is alright for now but I want it more dynamic than listing out every possible ways. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046562 Share on other sites More sharing options...
MadTechie Posted April 22, 2010 Share Posted April 22, 2010 maybe use str_ireplace instead Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046568 Share on other sites More sharing options...
kevin_newbie Posted April 22, 2010 Author Share Posted April 22, 2010 Why use the str_ireplace? Case sensitive? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046572 Share on other sites More sharing options...
cyberRobot Posted April 22, 2010 Share Posted April 22, 2010 Yeah the function is doing that but it breaks if there is something like "Mr." or "Dr." anything that is two letters with a "." in front of it. So then the function you're using doesn't break if there are more than 2 letters...such as "Mrs."? What if there is an ellipsis "..." in the middle of the sentence? Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046584 Share on other sites More sharing options...
kevin_newbie Posted April 22, 2010 Author Share Posted April 22, 2010 Okay tried the ellipsis and it works perfect for that part. But with "mrs." it stops at that word with the period. So it is broken for that part. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046607 Share on other sites More sharing options...
kevin_newbie Posted April 22, 2010 Author Share Posted April 22, 2010 I found this expression that involves dr, mr, mrs and so forth. <([DM][rs]{1,2}). Just having a hard time incorporating it with my current expressions. :/ Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046715 Share on other sites More sharing options...
dgoosens Posted April 23, 2010 Share Posted April 23, 2010 hi Kevin, I think the smartest way to do this is to check whether the "." is followed by a space AND an uppercase... Although... it gets harder when you have ...Dr. Newton... or something like this. In that case, I would try to get my hands on a list of common abbreviations. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046850 Share on other sites More sharing options...
ChemicalBliss Posted April 23, 2010 Share Posted April 23, 2010 <?php $skipped = array( "Mrs.", "Mr.", "Ms.", "Dr.", "PhD.", "Miss.", "St." ); $Sentence = "This is a sentence by Mr. Smith, and Mrs. Hannock. This is another sentence by Dr. Fred, Mrs. Wills, PhD. John and St. Peter. etc. Oh And Miss. Han."; // Encode Trick function encode_trick($skips,$string){ $Sentence = $string; $encoded = array(); foreach($skips as $item){ $encoded[] = str_replace(".",".",$item); } $Sentence = str_replace($skips,$encoded,$Sentence); $Sentence_Array = explode(". ",$Sentence); for($i=0;$i<count($Sentence_Array);$i++){ $Sentence_Array[$i] = str_replace(".",".",$Sentence_Array[$i]); } return $Sentence_Array; } // Placement Index function advanced_index($skips,$string){ $Sentence = $string; // Create the index $indexes = array(); foreach($skips As $item){ while(($index = strpos($Sentence, $item)) !== FALSE){ $indexes[] = array($index,$item); $Sentence = substr_replace($Sentence,"",$index,strlen($item)); } } // Get seperate Sentences $Sentence_Array = explode(". ",$Sentence); // Prepare variables for insertion $SA_Count = count($Sentence_Array); $indexes = array_reverse($indexes); $current_strlen = 0; // Loop each index that was stored Foreach($indexes As $idxarray){ // Then, Loop for each sentence for($i=0;$i<$SA_Count;$i++){ // Couple variables used in substr calculations $previous_strlen = $current_strlen; $current_strlen += strlen($Sentence_Array[$i]) + 2; // 2 is here for the dot + space we exploded ealier. // If the current str length is bigger than the index then insert in this sentence. if($current_strlen > $idxarray[0]){ // Get position $position = ($i > 0)? ($idxarray[0] - $previous_strlen) : $idxarray[0]; // Insert $Sentence_Array[$i] = substr_replace($Sentence_Array[$i],$idxarray[1],$position,0); break; } } // Reset for each index. $current_strlen = 0; } return $Sentence_Array; } // Tests print_r(encode_trick($skipped,$Sentence)); // The result. print_r(advanced_index($skipped,$Sentence)); // The result. ?> This should get you on your way, the first function is reliant on the fact that the text wont have that specific pattern in the there. The second has no limitations, and should work in any situation with any text of any patterns. -cb- Edit: You can replace strpos() with stripos() if you want to match any case. Same goes for str_replace(), you can use str_ireplace(); Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1046870 Share on other sites More sharing options...
kevin_newbie Posted April 24, 2010 Author Share Posted April 24, 2010 @ChemicalBliss - WOW thanks so much for all this. But the thing I am having in issue is how can I pick how many sentences I want and display it without saying "array"? Thank you once again and I really appreciate this. Quote Link to comment https://forums.phpfreaks.com/topic/199303-explode-a-but-ignore-dr/#findComment-1047446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.