tobeyt23 Posted August 22, 2007 Share Posted August 22, 2007 Pulling data from old DB to new DB. One field is text for an article, I need to pull it and only get part of it for a new summary field. How can I get just part, say 1 or 2 paragraphs out of the string? Thanks Quote Link to comment Share on other sites More sharing options...
hostfreak Posted August 22, 2007 Share Posted August 22, 2007 http://php.net/substr Edit- Unless of course you just want to do it with MySQL... im not sure about that Edit- NVM I remember, in MySQL you can use the LEFT function (http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left) Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted August 22, 2007 Author Share Posted August 22, 2007 Not exactly what I was looking for, would more like to break it on a sentence or paragraph end. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 22, 2007 Share Posted August 22, 2007 explode? Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted August 22, 2007 Author Share Posted August 22, 2007 Still not what I am looking for Quote Link to comment Share on other sites More sharing options...
thryb Posted August 22, 2007 Share Posted August 22, 2007 search your string for a "." (for sentence or the 10th "." depend on how many you want) and count the char before then substr it. that would be my idea n00bie idea Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted August 22, 2007 Author Share Posted August 22, 2007 What if the sentence doesn't end with '.'? Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted August 23, 2007 Author Share Posted August 23, 2007 I have done the above with substr, however it breaks sentences up and I am not looking for that. Would like to just grab first 4-5 sentences. The might end with "." or not. Any more suggestions? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 Well, if you mean that a sentence could end with a ?/!/. then i would suggest you have two options. 1.) Regular expressions. 2.) Cycle through the string character by character and break it apart where necessary. Since regular expressions are annoying me at the moment: <?php $str = "Hello! Is anybody out there? Obviously not. I'm all alone"; $i=0; $z = 0; $sentences = array(); while($i<strlen($str)){ $sentences[$z] .= $str[$i]; if($str[$i] == '!' || $strlen[$i] == '.' || $strlen[$i] == '?'){ $z++; } $i++; } print_r($sentences); ?> If thats not the issue, then im not sure how you would tell where a sentence ends. Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted August 27, 2007 Author Share Posted August 27, 2007 This what I ended up doing: <?php $str = "This is a sentence. This is a question? This is some excitment!"; $sentences = preg_split("/[.!?]/", $str, -1, PREG_SPLIT_OFFSET_CAPTURE); echo substr($str, 0, $sentences[1][1]); ?> Quote Link to comment 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.