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 Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/ 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) Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-331139 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. Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-331160 Share on other sites More sharing options...
GingerRobot Posted August 22, 2007 Share Posted August 22, 2007 explode? Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-331164 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 Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-331182 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 Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-331189 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 '.'? Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-331216 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? Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-332234 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. Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-333103 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]); ?> Link to comment https://forums.phpfreaks.com/topic/66201-using-only-part-of-a-string/#findComment-335318 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.