Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Text string to multiple lines

  1. #1
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Text string to multiple lines

    I have the follow code, but all it outputs is F

    PHP Code:
    <?php

    $str_value 
    "Feel free to use the regular|expression we made above on your own site to validate|email addresses, or modify it for your own purposes.";

    for(
    $i=0$i<sizeof($str_value); $i++) { 
        
    $line trim($str_value[$i]); 
        
    $arr explode("|",$line);
        
    $pg 3600;
        
    $pg += 100;
        
        
    $text ""implode($arr) .""
        echo 
    $text ."<br>\n";
        
    }

    ?>
    It should output:

    Feel free to use the regular
    expression we made above on your own site to validate
    email addresses, or modify it for your own purposes.

    Any ideas?
    Ryan
    Sevierville, TN

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Because instead of the = sign next to $arr, you should have put the += sign, I believe.

    -magicyte

  3. The Following User Says Thank You to magicyte For This Useful Post:

    NXArmada (09-19-2008)

  4. #3
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by magicyte View Post
    Because instead of the = sign next to $arr, you should have put the += sign, I believe.

    -magicyte
    I get this:
    Fatal error: Unsupported operand types
    Ryan
    Sevierville, TN

  5. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Uh, well I see what you mean when you say it prints "F". I tried it out, and it actually prints out "F" plus an added <br> (linebreak). I have one question. What is $pg for? BTW, I am a little bit new on PHP, but I CAN help, so what do ALL of these BUILT-IN functions do, ex explode, implode, trim, etc.?

    -magicyte

    P.S. VERY IMPORTANT!! I just noticed something I did wrong. PHP does allow the += sign. Sorry! Pre-define the $arr like this:

    $arr;

    then do this:

    $arr += explode("|",$line);

    That may work. Or, instead of +=, .= might work.
    Last edited by magicyte; 09-18-2008 at 08:32 PM.

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    += is allowed in PHP. It is, however, only for mathematical operations and has nothing to do with the script above. (unlike JS, you would use .= for strings.)

    One major problem here, and I don't know why it isn't giving you an error, is that you're not using implode('|',$arr);
    implode, I believe, requires a first operator of the "glue" to reconnect the strings. Feel free to use "" as an empty string if you want.

    However, overall, that doesn't do what you want. Using a for loop generally requires that you know how many splits you have first, or at least that you're using a value inside the loop to at some point change and terminate the loop. Neither is the case. It's surprising it isn't an infinite loop.

    Anyway, here's something that will work:

    Version 1:
    PHP Code:
    <?php
    $str 
    'abc|def|ghi';

    //simplest, just replace:
    $str str_replace('|',"\n",$str);
    echo 
    $str;
    ///OR
    //easy foreach loop:
    $str explode('|',$str);
    foreach (
    $str as $ln) {
    echo 
    $ln."\n";
    }
    ///OR
    //a more complex for loop:
    $str explode('|',$str);
    for (
    $i=0;$i<count($str);$i++) {
    echo 
    $str[$i]."\n";
    }

    ///OR
    //if you must avoid using explode:
    while (strpos($str,'|')!==FALSE) {
    echo 
    substr($str,0,strpos($str,'|'))."\n"//echo everything before the first |
    $str substr($str,strpos($str,'|')+1); //replace the string with everything after the first |
    }
    echo 
    $str."\n"//echo the last bit, outside of the loop, since there is no | now
    Last edited by djr33; 09-18-2008 at 08:42 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espaŮol | Deutsch | italiano | portuguÍs | catalŗ | un peu de franÁais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    NXArmada (09-19-2008)

  8. #6
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    the $pg is not used just something i put in to plan ahead if i can get this to work correctly.

    http://us.php.net/manual/en/function.explode.php
    http://us.php.net/manual/en/function.implode.php
    http://us.php.net/manual/en/function.trim.php
    Ryan
    Sevierville, TN

  9. #7
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by djr33 View Post
    += is allowed in PHP. It is, however, only for mathematical operations and has nothing to do with the script above. (unlike JS, you would use .= for strings.)

    One major problem here, and I don't know why it isn't giving you an error, is that you're not using implode('|',$arr);
    implode, I believe, requires a first operator of the "glue" to reconnect the strings. Feel free to use "" as an empty string if you want.
    Daniel I did what you suggested and no change.
    Ryan
    Sevierville, TN

  10. #8
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by magicyte View Post
    $arr += explode("|",$line);
    yeah that what i did when you first suggest += and the error is still the same
    Fatal error: Unsupported operand types
    Ryan
    Sevierville, TN

  11. #9
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Check out my post. I edited it. I have no whereabouts of what glue does, so, Daniel, I ask for help .

    -magicyte

  12. #10
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by magicyte View Post
    Check out my post. I edited it. I have no whereabouts of what glue does, so, Daniel, I ask for help .

    -magicyte
    if i do .= i get this

    Warning: implode() [function.implode]: Bad arguments.
    Ryan
    Sevierville, TN

Bookmarks

Posting Permissions

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