phpQuestioner Posted October 16, 2007 Share Posted October 16, 2007 how can you only pull out 3 words from a variable? then how would you pull 3 other words from your variable besides the original first 3 words? Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/ Share on other sites More sharing options...
sspoke Posted October 16, 2007 Share Posted October 16, 2007 you should say like.. how do you know they are words? do they have delimiters or maybe size related to split the words? I dont think php knows if they are words or not. or u mean http://us2.php.net/language.variables.variable ? Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370470 Share on other sites More sharing options...
tidalik Posted October 16, 2007 Share Posted October 16, 2007 Have you thought to turn your variable into an array using expode()? Then you just pull out what ever words you want, if you do it logically, you can keep track of the words already output. Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370471 Share on other sites More sharing options...
phpQuestioner Posted October 16, 2007 Author Share Posted October 16, 2007 not exactly what I meant; I know that I can use an explode, but I do not want to explode this variable until I get to a specific place in the variable. I would like to display the first 3 words in the variable and then display 3 more words from this variable in a different area of my page. like this: <?php echo "<table><td>"; $myvar="this is a string that I want to dissect "; $seperate = explode(" ", $myvar, 3); for($i = 0; $i < count($seperate); $i++){ echo "<li>$seperate[$i]</li>"; } echo "</td><td>"; // then some where else in my page pull 3 more addition words from this variable $myvar="this is a string that i want to dissect "; $seperate = explode(" ", $myvar, 3); for($i = 0; $i < count($seperate); $i++){ // only this time I want to echo the next 3 words from this variable here and exclude the 3 words above that I have already pulled out from this variable } echo "</td></table>"; ?> you see what I am saying..............? Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370475 Share on other sites More sharing options...
tidalik Posted October 16, 2007 Share Posted October 16, 2007 Why can't you just: <?php echo "<table><td>"; $myvar="this is a string that i want to dissect "; $seperate = explode(" ", $myvar); echo "<li>$seperate[0]</li><li>$seperate[1]</li><li>$seperate[2]</li>"; echo "</td><td>"; // then some where else in my page pull 3 more addition words from this variable // only this time I want to echo the next 3 words from this variable here echo "<li>$seperate[3]</li><li>$seperate[4]</li><li>$seperate[5]</li>"; } echo "</td></table>"; ?> Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370479 Share on other sites More sharing options...
sspoke Posted October 16, 2007 Share Posted October 16, 2007 <?php echo "<table><td>"; $myvar="this is a string that i want to dissect "; $seperate = explode(" ", $myvar); for($i = 0; $i < count($seperate); $i++){ echo "<li>$seperate[$i]</li><li>$seperate[$i+1]</li><li>$seperate[$i+2]</li>"; echo "</td><td>"; } echo "</td></table>"; ?> Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370481 Share on other sites More sharing options...
phpQuestioner Posted October 16, 2007 Author Share Posted October 16, 2007 probably because you left out the for loop; because I just tested that and there's an error in this syntax because noting displays Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370483 Share on other sites More sharing options...
phpQuestioner Posted October 16, 2007 Author Share Posted October 16, 2007 sspoke - I just tested your code to; their is an error in your syntax too. because nothing displays in the page with this code in it. Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370485 Share on other sites More sharing options...
tidalik Posted October 16, 2007 Share Posted October 16, 2007 Not quite understanding you there phpQuestioner... But I did forget to take out an extra } <?php echo "<table><td>"; $myvar="this is a string that i want to dissect "; $seperate = explode(" ", $myvar); echo "<li>$seperate[0]</li><li>$seperate[1]</li><li>$seperate[2]</li>"; echo "</td><td>"; // then some where else in my page pull 3 more addition words from this variable // only this time I want to echo the next 3 words from this variable here echo "<li>$seperate[3]</li><li>$seperate[4]</li><li>$seperate[5]</li>"; echo "</td></table>"; ?> Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370487 Share on other sites More sharing options...
sspoke Posted October 16, 2007 Share Posted October 16, 2007 <?php echo "<table><td>"; $myvar="this is a string that i want to dissect "; $seperate = explode(" ", $myvar); for($i = 0; $i < count($seperate); $i++){ echo "<li>".$seperate[$i]."</li><li>".$seperate[$i+1]."</li><li>".$seperate[$i+2]."</li>"; echo "</td><td>"; } echo "</td></table>"; ?> Fixed Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370488 Share on other sites More sharing options...
phpQuestioner Posted October 16, 2007 Author Share Posted October 16, 2007 Thank For Your Help Guys - I did get tidalik's version to work the way I wanted it to. Link to comment https://forums.phpfreaks.com/topic/73439-solved-how-to-select-only-a-specifc-amount-of-words-from-a-variable/#findComment-370489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.