ryancooper Posted May 6, 2011 Share Posted May 6, 2011 I'm using the code below to on echo certain words based on their index/key value, this works fine however, im trying to only do this in exploded quotes... the problem is, exploding quotes turns the portion into an array which cant be passed to str_word_count... how can i do the same thing only inside quotes. So that hello world "this is a test" would become: Hello World This [0] => Is [1] => a [2] => Test [3] Im trying to always remove say the 4th word in quotations, or maybe the 3rd and 4th, whatever i want to omit i leave out from the echo, so echo $data[0].' '.$data[3].'; would output "This Test" omitting "Is A" $data = 'hello world "this is a test"'; $data = str_word_count($data, 1); echo $data[1].' '.$data[2].' '.$data[0]; Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/ Share on other sites More sharing options...
Psycho Posted May 6, 2011 Share Posted May 6, 2011 Kind of hard to really provide a solution based on the information provided. Can there be multiple instances of quoted text in the string? Is there text before AND after the quoted string(s)? I would like to know more information before I invest time in writing any code. But, I think the approach I would take would be to use a regular expression to get the quoted text, split it using explode, remove the unwanted words and implode it back, and finally replace the original value with the modified text. Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211459 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 Not sure but do you mean something like this? <?php $data = 'hello world "this is a test"'; $RegEx = '/"[^"]*"/i'; if (preg_match($RegEx, $data, $regs)) { $quote = str_word_count($regs[0], 1); unset($quote[1]); //remove IS $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data); } echo $data; return hello world "this a test" Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211462 Share on other sites More sharing options...
ryancooper Posted May 6, 2011 Author Share Posted May 6, 2011 Kind of hard to really provide a solution based on the information provided. Can there be multiple instances of quoted text in the string? Is there text before AND after the quoted string(s)? I would like to know more information before I invest time in writing any code. But, I think the approach I would take would be to use a regular expression to get the quoted text, split it using explode, remove the unwanted words and implode it back, and finally replace the original value with the modified text. Thanks for your response, ive been working all week on a solution but usually people dont understand what im trying to do... There is only one set of quotes in the string, and there is always text before and after the quotes. I've tried messing with Regex a bit, the basics are easy enough but when you get it the more complex expressions its really overwhelming... It would work to split the matched string by spaces though, i assume thats what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211463 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 So is mine close ? yeah RegEx's are like that but the more you do the easier they become, then you tend to over then then LOL Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211466 Share on other sites More sharing options...
ryancooper Posted May 6, 2011 Author Share Posted May 6, 2011 So is mine close ? yeah RegEx's are like that but the more you do the easier they become, then you tend to over then then LOL Yeah i think your's is close im getting unexpected T_CONSTANT_ENCAPSED_STRING for your regex, im trying to figure out what quote is unescaped... Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211469 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 Just attached a file (tested), but here is the code <?php $data = 'hello world "this is a test" last test'; $RegEx = '/"[^"]*"/i'; if (preg_match($RegEx, $data, $regs)) { $quote = str_word_count($regs[0], 1); unset($quote[1]); //remove IS $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data); } echo $data; OUTPUT was: hello world "this a test" last test [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211471 Share on other sites More sharing options...
ryancooper Posted May 6, 2011 Author Share Posted May 6, 2011 Just attached a file (tested), but here is the code <?php $data = 'hello world "this is a test" last test'; $RegEx = '/"[^"]*"/i'; if (preg_match($RegEx, $data, $regs)) { $quote = str_word_count($regs[0], 1); unset($quote[1]); //remove IS $data = str_replace($regs[0], '"'.implode(' ', $quote).'"', $data); } echo $data; OUTPUT was: hello world "this a test" last test I copied the code again, same problem, however the attached code runs fine. That works perfect, thanks so much for your help, you made it seem easy, ive had quite a few people try and not be able to help so i really appreciate it. Could i change unset($quote[1]); to strtoupper($quote[1]); to say make the second word all uppercase? Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211474 Share on other sites More sharing options...
MadTechie Posted May 6, 2011 Share Posted May 6, 2011 Yes,$quote is every word in the quoted area, so unset, shuffle, whatever Im glade I could help Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211477 Share on other sites More sharing options...
Psycho Posted May 6, 2011 Share Posted May 6, 2011 I created a function that allows you to determine which words to retain within the quoted text based upon the index. function removeQuotedWords($string, $retainIdxAry) { $matched = preg_match('#"([^"]*)"#', $string, $quotedText); if($matched == 0) { return false; } $quotedWordsAry = explode(' ', $quotedText[1]); $replacWordsAry = array_intersect_key($quotedWordsAry, array_flip($retainIdxAry)); $replaceTextStr = implode(' ', $replacWordsAry); return preg_replace('#"([^"]*)"#', "\"{$replaceTextStr}\"", $string); } $data = 'hello world "this is a test" after text' echo removeQuotedWords($data, array(0,3)); //Output: hello world "this test" after text Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211479 Share on other sites More sharing options...
ryancooper Posted May 6, 2011 Author Share Posted May 6, 2011 Yes,$quote is every word in the quoted area, so unset, shuffle, whatever Im glade I could help I'm not sure why, but using strtoupper doesnt seem to work when replacing unset? Any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211486 Share on other sites More sharing options...
ryancooper Posted May 6, 2011 Author Share Posted May 6, 2011 I created a function that allows you to determine which words to retain within the quoted text based upon the index. function removeQuotedWords($string, $retainIdxAry) { $matched = preg_match('#"([^"]*)"#', $string, $quotedText); if($matched == 0) { return false; } $quotedWordsAry = explode(' ', $quotedText[1]); $replacWordsAry = array_intersect_key($quotedWordsAry, array_flip($retainIdxAry)); $replaceTextStr = implode(' ', $replacWordsAry); return preg_replace('#"([^"]*)"#', "\"{$replaceTextStr}\"", $string); } $data = 'hello world "this is a test" after text' echo removeQuotedWords($data, array(0,3)); //Output: hello world "this test" after text Awesome, i can actually use this is a different instance, i appreciate it! Thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/235704-get-word-count-index-within-exploded-array/#findComment-1211489 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.