Drompo2 Posted July 25, 2011 Share Posted July 25, 2011 I have an Array of data of words and where they are positioned in a string of data. Array ( [0] => 0|That [1] => 1|hat[2] =>2|at [3] => 13|That [4] => 14|hat [5] =>15|at [6] => 18|Cat [7] => 19|at [8] => 28|swordsman [9] => 28|sword [10] => 28|swords [11] => 34|man) That man with that cat was a swordsman Is there anyway to display these words above/below the string in the correct position? Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/ Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 You could do something like this.. If you have anotehr array with the proper order with the same sentence, then you can take its order and rearrange your other array based on that Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1246991 Share on other sites More sharing options...
AbraCadaver Posted July 25, 2011 Share Posted July 25, 2011 You'll have to be more specific. The string you posted makes no sense to me given the array. Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247002 Share on other sites More sharing options...
Drompo2 Posted July 25, 2011 Author Share Posted July 25, 2011 Sorry about the vagueness. It is searching for words in the string that contain other words. For example, 'That' contains 'hat', and 'at'. 'Swordsman' contains 'Sword', 'Swords', 'words', 'man' The Array Number was slightly off for some reason too That man with that cat was a swordsman Array ( [0] => 0|That [1] => 1|hat[2] =>2|at [3] => 14|That [4] => 15|hat [5] =>16|at [6] => 18|Cat [7] => 19|at [8] => 28|swordsman [9] => 28|sword [10] => 28|swords [11] => 34|man) Does that help you anymore? Any help much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247049 Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 Sorry about the vagueness. It is searching for words in the string that contain other words. For example, 'That' contains 'hat', and 'at'. 'Swordsman' contains 'Sword', 'Swords', 'words', 'man' Does that help you anymore? Any help much appreciated See thats a completely different story, you can use the in_array function Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247051 Share on other sites More sharing options...
Drompo2 Posted July 25, 2011 Author Share Posted July 25, 2011 Sorry about the vagueness. It is searching for words in the string that contain other words. For example, 'That' contains 'hat', and 'at'. 'Swordsman' contains 'Sword', 'Swords', 'words', 'man' Does that help you anymore? Any help much appreciated See thats a completely different story, you can use the in_array function Thanks for the quick reply, I know that it exists in the array, thats the point i have got upto, its displaying the word above the string to show it contains other words that im stuck on :/ For example... swords <-- Containing word, Had to go on another line because of clash man <-- Containing word swordsman <-- String sword <-- Containing word Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247054 Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 Then you need to rearrange your Array to me more 2-3 dimensional for sub words.. Why did you do this 1|hat ? Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247057 Share on other sites More sharing options...
Drompo2 Posted July 25, 2011 Author Share Posted July 25, 2011 Then you need to rearrange your Array to me more 2-3 dimensional for sub words.. Why did you do this 1|hat ? Right, i will try that. And because It takes the first character as posistion 0, and then the second character as position 1. Therefore the word hat is found at Position 1. Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247059 Share on other sites More sharing options...
phpSensei Posted July 25, 2011 Share Posted July 25, 2011 Once you create an array for the Phrase, the array will already set indexes for the words, next you need to get rid of this 1|HAT... Then compare any array against the original phrase's array and match the words then set the index for the array. hopefully thats not too confusing. Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247066 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 I have been trying to work with your code and make it work for about 30 minutes now. Here is what I have come up with. Its a little sketchy. <?php $words = array ( "0|That", "1|hat", "2|at", "14|that", "15|hat", "16|at", "19|cat", "20|at", "29|swordsman", "29|sword", "29|swords", "34|man"); $string="That man with that cat was a swordsman"; $stringWords=explode(" ", $string); $wordcount=count($stringWords); $wordBlocks=array(); // Print String $stringPosition=0; foreach ($stringWords as $key => $wordValue) { $wordStop=$stringPosition+strlen($wordValue); echo "<div style=\"float: left;\">{$wordValue} <br />"; foreach ($words as $word) { $w=explode("|", $word); if (($w[0] >= $stringPosition)&&($w[0] < $wordStop)) { echo "<span style=\"color: #FFFFFF;\">"; echo preg_replace("@(". $w[1] .")@", "<span style='color: #000000;'>$1</span>", $wordValue); echo "</span><br />"; } } echo "</div>"; $stringPosition+=strlen($wordValue)+1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247072 Share on other sites More sharing options...
teynon Posted July 25, 2011 Share Posted July 25, 2011 Update: I changed the span's to hidden to hide the other text. <?php $words = array ( "0|That", "1|hat", "2|at", "14|that", "15|hat", "16|at", "19|cat", "20|at", "29|swordsman", "29|sword", "29|swords", "34|man"); $string="That man with that cat was a swordsman"; $stringWords=explode(" ", $string); $wordcount=count($stringWords); $wordBlocks=array(); // Print String $stringPosition=0; foreach ($stringWords as $key => $wordValue) { $wordStop=$stringPosition+strlen($wordValue); echo "<div style=\"float: left;\">{$wordValue} <br />"; foreach ($words as $word) { $w=explode("|", $word); if (($w[0] >= $stringPosition)&&($w[0] < $wordStop)) { $spaces=$w[0]-$stringPosition; echo "<span style=\"visibility: hidden;\">"; echo preg_replace("@(". $w[1] .")@", "<span style='visibility: visible;'>$1</span>", $wordValue); echo "</span><br />"; } } echo "</div>"; $stringPosition+=strlen($wordValue)+1; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247082 Share on other sites More sharing options...
Drompo2 Posted July 26, 2011 Author Share Posted July 26, 2011 That works brilliently, thank you Sorry to be a pain, dont suppose there is away to find a ward if there is a gap inbetween. For example 'Mass election' would find mass, election but also selection Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247339 Share on other sites More sharing options...
teynon Posted July 26, 2011 Share Posted July 26, 2011 When you create your array, search for it like this: $string=str_replace(" ", "", $string); then compare however you are doing it. If its in there, split up the word in the array. (Find 10|s, 12|election) Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247362 Share on other sites More sharing options...
Drompo2 Posted July 26, 2011 Author Share Posted July 26, 2011 Yeah i had tried a similar solution to that, its just that it then outputs everything on different lines Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247363 Share on other sites More sharing options...
teynon Posted July 26, 2011 Share Posted July 26, 2011 How do you want it to display? If you try to display it, the letters won't line up correctly. If you wanted, you could append the s with a - when its separated so when it prints out the word it would look like this: Mass election s-election Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247365 Share on other sites More sharing options...
Drompo2 Posted July 26, 2011 Author Share Posted July 26, 2011 How do you want it to display? If you try to display it, the letters won't line up correctly. If you wanted, you could append the s with a - when its separated so when it prints out the word it would look like this: Mass election s-election Ah yeah of course, it doesnt matter then, was just an after thought i had Quote Link to comment https://forums.phpfreaks.com/topic/242788-display-multiple-line-text-in-tree-formation/#findComment-1247369 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.