#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0

    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. 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
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Location
    Worldwide
    Posts
    49
    Rep Power
    45
    Are their supposed to be random spaces between some of the numbers or did you just mess it up? Start here.

    PHP Code:
    <?php
    $content 
    "word1 word2 word3 word4 word5"
    $pieces explode(" "$content);
                  
    foreach( 
    $pieces as $value ){
        echo 
    $value."\n";
    }
    Returns

    word1
    word2
    word3
    word4
    word5

    Comments on this post

    • UniqueIdeaMan agrees : http://forums.devshed.com/php-development/979832-line-word-own-line-post2982763.html#post2982763
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by benanamen
    Are their supposed to be random spaces between some of the numbers or did you just mess it up? Start here.

    PHP Code:
    <?php
    $content 
    "word1 word2 word3 word4 word5"
    $pieces explode(" "$content);
                  
    foreach( 
    $pieces as $value ){
        echo 
    $value."\n";
    }
    Returns

    word1
    word2
    word3
    word4
    word5
    Ben,

    For some reason, using your code I see this:

    word1 word2 word3 word4 word5

    The words are not lined-up by themselves on each line like so:

    word1
    word2
    word3
    word4
    word5

    And yes, I did mess things up by leaving spaces on some and no spaces on others.
    Anyway, I experimented like this now but no luck:

    PHP Code:
    <?php 

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

    <body>
    Benanaman is a helpful bright lad!<br>
    That is why everone likes him everywhere.<br>
    Not flattering, It is the truth!<br>
    </body>
    </html>"
    ;

    $pieces explode(" "$content);
                  
    foreach( 
    $pieces as $value ){
        echo 
    $value."\n";
    }
    ?>
    I see like this:

    Benanaman is a helpful bright lad!
    That is why everone likes him everywhere.
    Not flattering, It is the truth!


    It should have lined-up like this ...

    Benanaman
    is
    a
    helpful
    bright
    lad!
    That
    is
    why
    everone
    likes
    him everywhere.
    Not
    flattering,
    It
    is
    the
    truth!
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Location
    Worldwide
    Posts
    49
    Rep Power
    45
    It does lineup the way you asked for. Try viewing the source code. You didn't say anywhere that you were doing this in HTML. Replace the /n with a <br>

    You still need some practice on how to ask a question.

    Comments on this post

    • UniqueIdeaMan agrees : http://forums.devshed.com/php-development/979832-line-word-own-line-post2982771.html#post2982771
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by benanamen
    It does lineup the way you asked for. Try viewing the source code. You didn't say anywhere that you were doing this in HTML. Replace the /n with a <br>

    You still need some practice on how to ask a question.
    Benanaman,

    Excellent! I should have checked the Source Code!
    Gave you a REP!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0

    Question


    Ok. Now, let us assume that, I need to have more than one delimiter in the explode function's param 1. How would I do it ?

    So, I need to separate the words after each of the following:

    .
    ,
    !
    ?

    That is a full stop, comma, exclamation mark and a question mark.
    So, let's explode the following content:

    Benanaman still prefers formality. So, let's stick to it! Shall we ? That's a good chap.

    That should be lined-up like this when checking the source code:

    Benanaman still prefers formality.
    So,
    let's stick to it!
    Shall we ?
    That's a good chap.


    I coded it like this. But get error:

    PHP Code:
    <?php 

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

    <body>
    Benanaman still prefers formality.<br>
    So, let's stick to it! Shall we ?<br>
    That's a good chap.
    </body>
    </html>
    "
    ;

    $pieces_1 explode("."$content);
    $pieces_2 explode(","$pieces_1);
    $pieces_3 explode("!"$pieces_2);
    $pieces_4 explode("?"$pieces_3);

    foreach(
    $pieces_4 as $value
    {
            echo 
    $value."\n";
    }

    ?>
    Error I get:

    Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\project\explode.php on line 16

    Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\project\explode.php on line 18

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\project\explode.php on line 20


    Q1.
    Ok. I understand the 2nd parameter of the explode() can't be an array. Has to be a string. Guessing TypeCasting the 2nd param won't do.
    But, why the error in the "foreach" ? How is it an invalid argument ?

    Q2.
    How to have more than one delimiter in the explode() ? In short, how to feed explode() the ".", ",", "!" and the "?" in the 2nd parameter.
    Must link the 2nd param to an Array. Right ? Must loop through the Array, correct ?
    Now, can someone be kind enough to remind me how to do that with a code sample? I've forgotten it! Eek!
    I am getting into making attempts now but to save time I prefer a pro show me a typical example.

    Thanks!

    EDIT:

    I tried the following but did not work. Even though I get no errors.

    PHP Code:
    <?php 

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

    <body>
    Benanaman still prefers formality.<br>
    So, let's stick to it! Shall we ?<br>
    That's a good chap.
    </body>
    </html>
    "
    ;

    $pieces_1 explode("."$content);
    $pieces_2 explode(","$content);
    $pieces_3 explode("!"$content);
    $pieces_4 explode("?"$content);

    foreach(
    $pieces_4 as $value
    {
            echo 
    $value."\n";
    }

    ?>
    Checking the source code I see this ...
    <html>
    <head><title>Article</title></head>

    <body>
    Benanaman still prefers formality.<br>
    So, let's stick to it! Shall we
    <br>
    That's a good chap.
    </body>
    </html>


    And, in the browser, I see this:

    Benanaman still prefers formality.
    So, let's stick to it! Shall we
    That's a good chap.


    Q3.
    Note that, both in the browser and in the source code, I see the question mark has vanished after "Shall we". Why is that ?

    I guess, I'll have to find the answer to my Q2.
    Last edited by UniqueIdeaMan; January 15th, 2018 at 08:49 AM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    Originally Posted by UniqueIdeaMan
    Q1.
    Ok. I understand the 2nd parameter of the explode() can't be an array. Has to be a string.
    But, why the error in the "foreach" ? How is it an invalid argument ?
    Think about it. What do you think '$peices_4' contains since there was an error setting it?
    Originally Posted by UniqueIdeaMan
    Q2.
    How to have more than one delimiter in the explode() ? In short, how to feed explode() the ".", ",", "!" and the "?" in the 2nd parameter.
    Must link the 2nd param to an Array. Right ? Must loop through the Array, correct ?
    Now, can someone be kind enough to remind me how to do that with a code sample? I've forgotten it! Eek!
    I am getting into making attempts now but to save time I prefer a pro show me a typical example.

    Thanks!

    Use a regular expression and since you don't know how to use search engines, the answer to your next question is preg_split.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by gw1500se
    Think about it. What do you think '$peices_4' contains since there was an error setting it?



    Use a regular expression and since you don't know how to use search engines, the answer to your next question is preg_split.
    Thanks. I checked the link. This ain't working for me:

    PHP Code:
    $pieces preg_split("/[\s,]+/""$content"); 
    I tried learning REGEX but too complicated even for pros.
    So, according to you, what should the REGEX look like ? I need the following as the 2nd parameter of the explode():

    .
    ,
    !
    ?

    Yes, you are correct. $pieces has no value. I should have figured that out by myself. But, I did not.
    Last edited by UniqueIdeaMan; January 15th, 2018 at 09:05 AM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    Yes, regexp is not easy to learn, especially for someone who struggles with the simplest of PHP concepts. It takes a lot of effort. There is a regexp forum here but first you might try this.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by gw1500se
    Yes, regexp is not easy to learn, especially for someone who struggles with the simplest of PHP concepts. It takes a lot of effort. There is a regexp forum here but first you might try this.
    Thanks for the link. All the regex sites so far that got suggested to me few yrs back (when learning to build the .exe bots) were too complicated. I have a feeling your suggested link would fair me better!
    But still. Saying all this and all. I'm still gonna go to devshed regex section and ask these same questions and more on regex to try and get someone to just hand me the regex code so I don't have to waste my time pulling my hair trying to figure-out the right regex for my current project. Lol!
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    You probably should use the hire a programmer forum. These forums are for getting advice and help not for free programming.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by gw1500se
    You probably should use the hire a programmer forum. These forums are for getting advice and help not for free programming.
    I did never notice that page.
    Ha! Ha! Ha! Nah! I ain't hiring, my man!
    I'm asking you guys and learning from you guys or atleast learning which php function is appropriate for me so I can research on it and learn it.
    And the code sample you guys give is also for learning purposes. And not just mine. Others who read my threads will also learn from your snippets.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    Then learn to program regex's and let us know what you tried and what you got that you didn't expect or didn't work. You learn nothing when someone else does the work.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by gw1500se
    Then learn to program regex's and let us know what you tried and what you got that you didn't expect or didn't work. You learn nothing when someone else does the work.
    I float to the forums when I get stuck or have no clue where to begin. That is when I ask for a little code sample to get me up and running.
    I saw you asking regex questions and Requinix answering. So, you are a student, like me then ? I thought you were a pro web developer with your own business or employed in the php programming.
    Good to have another student replying to my posts. But, I'd prefer if you provide code samples in your replies to my posts. And then see how I modify them. That way, you can give me score on how well I learnt things from your sample codes. (Not being cheeky!). Lol!
    Afterall, you never know when you'll hit the jackpot. I might, as a return favour, give you some "unique ideas" or "near unique" and you might make your first $million.
    Gonna check you out now in your profile for your website address to see what kind of internet venture you're upto. I''m good at being creative. You should know that by now. Lol!

    Thanks
    Last edited by UniqueIdeaMan; January 16th, 2018 at 09:13 AM.

IMN logo majestic logo threadwatch logo seochat tools logo