Webmaster Sun Login
Not a member yet? Sign up

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

  • HOME
  • FORUMS
  • BLOGS
  • MARKETPLACE
  • ADVERTISING
  • SPECIAL OFFERS
  • WEB HOSTING
  • QUICK MENU
  • REGISTER HERE - Join us for FREE
Results 1 to 9 of 9
    Stick this thread
  1. #1
    Join Date
    Jun 2017
    Posts
    86
    Thumbs Up/Down
    Received: 0/0
    Given: 22/0
    Thanks
    13
    Thanked 1 Time in 1 Post

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

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

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

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

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

    echo 
    "words: $words";

    ?>

    Thanks

  2. #2
    Join Date
    May 2016
    Location
    USA
    Posts
    1,406
    Thumbs Up/Down
    Received: 423/1
    Given: 27/1
    Thanks
    13
    Thanked 176 Times in 161 Posts
    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.

  3. #3
    Join Date
    Jun 2017
    Posts
    86
    Thumbs Up/Down
    Received: 0/0
    Given: 22/0
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Rob Whisonant View Post
    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.
    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.

  4. #4
    Join Date
    May 2016
    Location
    USA
    Posts
    1,406
    Thumbs Up/Down
    Received: 423/1
    Given: 27/1
    Thanks
    13
    Thanked 176 Times in 161 Posts
    Quote Originally Posted by sunny_pro View Post
    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.
    That is why I said use a foreach loop. It does not need to know the number of elements. It will loop through each until it hits the end.

  5. #5
    Join Date
    Jun 2017
    Posts
    86
    Thumbs Up/Down
    Received: 0/0
    Given: 22/0
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Rob Whisonant View Post
    That is why I said use a foreach loop. It does not need to know the number of elements. It will loop through each until it hits the end.
    What do you think about this ?

    PHP Code:
    PHP Code:
    <?php 

    $content 
    "<html>
    <head><title>Article</title></head>

    <body>
    webmastersun.com is a helpful forum!<br>
    That is why everone likes it here.<br>
    Not flattering, it is the truth!<br>
    </body>
    </html>"
    ;

    $pieces explode(" "$content);
                  
    foreach( 
    $pieces as $value ){
        echo 
    $value."\n";
    }
    ?>

  6. #6
    Join Date
    May 2016
    Location
    USA
    Posts
    1,406
    Thumbs Up/Down
    Received: 423/1
    Given: 27/1
    Thanks
    13
    Thanked 176 Times in 161 Posts
    Quote Originally Posted by sunny_pro View Post
    What do you think about this ?

    PHP Code:
    PHP Code:
    <?php 

    $content 
    "<html>
    <head><title>Article</title></head>

    <body>
    webmastersun.com is a helpful forum!<br>
    That is why everone likes it here.<br>
    Not flattering, it is the truth!<br>
    </body>
    </html>"
    ;

    $pieces explode(" "$content);
                  
    foreach( 
    $pieces as $value ){
        echo 
    $value."\n";
    }
    ?>
    Much better. Personally I would use something like strip_tags on the string before you explode it. That way you only have the text and not the HTML tags. But be careful of pages with a broken tag. Validate the html first. If it does not validate, skip the page and don't index it. etc.

  7. #7
    Join Date
    Jun 2017
    Posts
    86
    Thumbs Up/Down
    Received: 0/0
    Given: 22/0
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Rob Whisonant View Post
    Much better. Personally I would use something like strip_tags on the string before you explode it. That way you only have the text and not the HTML tags. But be careful of pages with a broken tag. Validate the html first. If it does not validate, skip the page and don't index it. etc.
    Ah! Thank you very much for speaking your mind out aloud. Therefore, care to be kind enough to add the strip tag yourself so I can get a hunch how it should be done ? I should be able to do things from then on. Other thread readers would benefit too.

  8. #8
    Join Date
    May 2016
    Location
    USA
    Posts
    1,406
    Thumbs Up/Down
    Received: 423/1
    Given: 27/1
    Thanks
    13
    Thanked 176 Times in 161 Posts
    Quote Originally Posted by sunny_pro View Post
    Ah! Thank you very much for speaking your mind out aloud. Therefore, care to be kind enough to add the strip tag yourself so I can get a hunch how it should be done ? I should be able to do things from then on. Other thread readers would benefit too.
    $content = strip_tags($content);

  9. The Following User Says Thank You to Rob Whisonant For This Useful Post:
    sunny_pro (01-17-2018)

  10. #9
    Join Date
    Jun 2017
    Posts
    86
    Thumbs Up/Down
    Received: 0/0
    Given: 22/0
    Thanks
    13
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Rob Whisonant View Post
    $content = strip_tags($content);
    Thanks for reminding!

Newer Threads

  1. klarencekay28
    bijutoha
    Replies: 3 | Views: 274
    Last post by bijutoha, 01-19-2018, 05:02 AM
  2. DanielN
    Judas2018
    Replies: 2 | Views: 303
    Last post by Judas2018, 01-17-2018, 11:23 PM
  3. sunny_pro
    Netman82
    Replies: 3 | Views: 395
    Last post by Netman82, 02-03-2018, 02:17 PM
  4. sunny_pro
    sunny_pro
    Replies: 2 | Views: 280
    Last post by sunny_pro, 01-17-2018, 02:43 PM
  5. lasostashop1
    SEOPub
    Replies: 9 | Views: 573
    Last post by SEOPub, 04-04-2018, 10:13 PM

Older Threads

  1. Gsain123
    Hello
    By Gsain123 in forum New Member Introductions
    ulterios
    Replies: 3 | Views: 228
    Last post by ulterios, 01-14-2018, 06:07 AM
  2. visalprom
    Introduction
    By visalprom in forum New Member Introductions
    ulterios
    Replies: 3 | Views: 256
    Last post by ulterios, 01-14-2018, 06:08 AM
  3. Mary4
    Hello,
    By Mary4 in forum New Member Introductions
    Rob Whisonant
    Replies: 2 | Views: 200
    Last post by Rob Whisonant, 01-12-2018, 08:45 PM
  4. NiNi Sweetie
    Hello
    By NiNi Sweetie in forum New Member Introductions
    ulterios
    Replies: 2 | Views: 206
    Last post by ulterios, 01-12-2018, 07:38 PM
  5. Ratha1207
    ivanab
    Replies: 6 | Views: 451
    Last post by ivanab, 01-15-2018, 01:26 PM

Latest Threads

  1. TheLarbyAMR
    TheLarbyAMR
    Replies: 0 | Views: 1
    Last post by TheLarbyAMR, Today, 01:12 PM
  2. databaseme
    smsmbooster
    Replies: 2 | Views: 96
    Last post by smsmbooster, Yesterday, 08:09 AM
  3. maverickwilliams
    Introduction
    By maverickwilliams in forum New Member Introductions
    DarranD
    Replies: 2 | Views: 90
    Last post by DarranD, 06-22-2018, 03:03 PM
  4. KaapoJed
    Mike001
    Replies: 1 | Views: 101
    Last post by Mike001, 06-21-2018, 03:34 PM
  5. harrygreen90
    harrygreen90
    Replies: 0 | Views: 73
    Last post by harrygreen90, 06-21-2018, 03:21 AM

Similar Threads

  1. jamesanderson1
    kim16
    Replies: 2 | Views: 2108
    Last post by kim16, 02-07-2017, 09:35 AM
  2. harrygreen90
    Mike_Brown
    Replies: 4 | Views: 1215
    Last post by Mike_Brown, 05-05-2016, 02:59 PM
  3. Dopani
    arindamb
    Replies: 1 | Views: 680
    Last post by arindamb, 12-30-2015, 02:16 AM
  4. KellyLC
    vaneetagoswami
    Replies: 2 | Views: 3353
    Last post by vaneetagoswami, 12-01-2014, 05:31 AM
  5. Jarvis
    roland
    Replies: 4 | Views: 1982
    Last post by roland, 11-11-2013, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Internet Marketing Forum

Webmaster Sun is a forum where you’ll find in-depth discussions and resources to help you succeed on the web whether you are new or experienced. You’ll find it all here. With topics ranging from internet marketing, search engine optimization, social networking, make money online, web hosting, affiliate marketing as well as hands-on technical support for web design, programming and more. We are a growing community of like-minded people that is keen to help and support each other with ambitions and online endeavors. Learn and grow, make friends and contacts for life.

Come Hang Out With Us

    Facebook Twitter Webmaster Forum Google+

    Webmaster Sun Logo
Copyright ©2012 - 2018, WebmasterSun.com. All rights reserved. Internet marketing forum for internet marketers, webmasters, web hosting providers, designers and affiliate marketers.

Welcome to Webmaster Sun

The World's Number 1 Webmaster Community, SEO and Marketplace

Log in!

Continue with Facebook
Continue With Email. By signing up you indicate that you have read and agree to the Terms of Service and Privacy Policy.

Sign in Manually

Need an account? Sign up now!