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

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
How come the $words variable contain the value "1" ?

PHP:
<?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:
<?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:
<?php 

$content = "word1 word 2 word3 word 4 word5";
echo "Content: $content<br>";

$words = (explode(" ", $content));

echo "words: $words";

?>

Thanks :)
 

Rob Whisonant

Moderator
Joined
May 24, 2016
Messages
2,489
Points
113
Use a foreach loop to loop through the elements of the array. That way you can insert all the formatting you want between each array element.
 

sunny_pro

New member
Joined
Jun 18, 2017
Messages
86
Points
0
sunny_pro
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.
 
Older threads
Replies
3
Views
1,506
Replies
3
Views
1,541
Replies
2
Views
1,390
Replies
2
Views
1,466
Replies
6
Views
2,507
Latest threads
Recommended threads
WTS ukads.org
Replies
1
Views
2,750
Replies
1
Views
2,104
Replies
7
Views
4,142
Replies
1
Views
3,497
Replies
10
Views
4,643

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top