Jump to content
Killersites Community
Sign in to follow this  
saversites

How To Line Each Word On It's Own Line ?

Recommended Posts

How come the $words variable contain the value "1" ?

<?php 
	$content = "word1 word 2 word3 word 4 word5";
echo "Content: $content<br>";
	$words = print_r(explode(" ", $content));
	echo "words: $words";
?>

I see this:

Content: word 1 word 2 word 3 word 4 word 5
Array ( [0] => word 1 [1] => word [2] => 2 [3] => word 3 [4] => word [5] => 4 [6] => word 5 ) words: 1


Anyway, I am trying to get each word from $content lined up like this:

word 1 
word 2 
word 3 
word 4 
word 5

How do I do it ? Any code sample appreciated. ;)

Replacing the print_r with var_dump not good, either. :chomp: As I see this:

Content: word1 word 2 word3 word 4 word5
array(7) { [0]=> string(5) "word1" [1]=> string(4) "word" [2]=> string(1) "2" [3]=> string(5) "word3" [4]=> string(4) "word" [5]=> string(1) "4" [6]=> string(5) "word5" } words:

No good:

<?php 
	$content = "word1 word 2 word3 word 4 word5";
echo "Content: $content<br>";
	$words = var_dump(explode(" ", $content));
	echo "words: $words";
?>

Neither no good just echoing the $words value as I see this:

Content: word1 word 2 word3 word 4 word5

Notice: Array to string conversion in C:\xampp\htdocs\project\explode.php on line 8
words: Array

And, no good this, either:

<?php 
	$content = "word1 word 2 word3 word 4 word5";
echo "Content: $content<br>";
	$words = (explode(" ", $content));
	echo "words: $words";
?>


Thanks :)

Edited by saversites

Share this post


Link to post
Share on other sites

Problem is, I don't know how many words there would exist on a page and therefore don't know how many arrays to create for each word.
In my example above, I just used 5 words but in reality I won't know how many words would exist on a page.
Trying to build a web crawler. So far, getting cURL to fetch the pages found on my db (user submissions).
Now, php needs to break each sentence into words found on the fetched page (for my crawler to learn what keywords make-up the page) and line them up one by one. Like so:

1st word
2nd word
3rd word

and so on for each and every word.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×