Kane250 Posted April 3, 2009 Share Posted April 3, 2009 Hi, I'm using the following code to select a paragraph of text, and split the paragraph up into sentences, but it's far from working perfectly. Every time it runs, I am getting a trailing blank entry in MySQL. I'm also not getting the periods from the sentences. Any idea how I can fix this? Also, is there an easy reg exp that I could use so it will separate sentences by (. , !, and ?) and tell it to ignore multiple line endings like: ... or ?? ? //Collect data from paragraph and put into a variable $para1content = ($_POST['finalFirstPara']); //Separate data into sentences, spearated by (.) and put into an array $para1exploded = (explode('.', $para1content)); foreach ($para1exploded as $content) //This iterates through the selected exploded array { $query = "INSERT INTO `para1` (`text`, `key`, `added_at`) VALUES ('$content', '$key', CURRENT_TIMESTAMP)"; if (!mysql_query($query,$conn)) { die('Error=' . mysql_error()); } }; Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/ Share on other sites More sharing options...
jackpf Posted April 3, 2009 Share Posted April 3, 2009 What happens if you echo the variable before exploding it? And in response to your regex idea, you may want to take a look at preg_split()... Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-800646 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 array_pop When you explode an array it will default the last item (given the sentences) to a black space, if you are splitting something where the option to split by is the last character. Using the above will remove that issue. As for the sentence issue, I am not sure what you are trying to get at. EDIT: Incase you may have a scenario where there is no ending period, you may want to do this over array_pop <?php foreach ($para1exploded as $content) //This iterates through the selected exploded array { if (empty($content)) continue; $query = "INSERT INTO `para1` (`text`, `key`, `added_at`) VALUES ('$content', '$key', CURRENT_TIMESTAMP)"; ?> That will prevent from anything that does not have characters from being entered into the DB. Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-800648 Share on other sites More sharing options...
jackpf Posted April 3, 2009 Share Posted April 3, 2009 Btw, just out of pure wonder, what is the difference between end() and array_pop()? Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-800653 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 End returns the last element of an array and moves the array pointer to the end (but that element stays inside the array). Array_pop removes the last element of the array completely. Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-800661 Share on other sites More sharing options...
jackpf Posted April 3, 2009 Share Posted April 3, 2009 Aha, makes sense. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-800662 Share on other sites More sharing options...
Kane250 Posted April 10, 2009 Author Share Posted April 10, 2009 Sorry for the delayed response. This looks good. I'm gonna try this today and see if it does what I need it to! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-806777 Share on other sites More sharing options...
Kane250 Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks guys, I actually ended up using split('[.!?] ', $variable), which worked well and also allows sentences to be types in with additional periods like this... However, in a normal sentence with just one period, it is still omitting that period. Anyway I can have it include a period that ends a sentence, but still include them when someone types extras? Quote Link to comment https://forums.phpfreaks.com/topic/152454-explode-help/#findComment-806908 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.