Jump to content

Display multiple line text in tree formation


Drompo2

Recommended Posts

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?

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  :confused:

 

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

 

 

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

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

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.

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.

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;
}

?>

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;
}

?>

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.