SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2001
    Location
    Kansas City, MO
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help with removing a tab at the beginning of an array

    Okay, I am getting closer:

    This is the code:

    Code:
    <?php
        $firstList = explode("\r", $_POST['listOne']); 
        $secondList = explode("\n", $_POST['listTwo']);
    ?>
    
        <textarea name="BroadPhraseExact" id="BroadPhraseExact" cols="45" rows="15">
        <?php foreach ($firstList as $value) {
                     $first = $value;
                     foreach ($secondList as $value) {
                        $second = $value;
                        echo $first. " " .$second;
                     }
                  } 
           ?>
        </textarea>
    This is the output:

    Code:
          One Two
    One Four
    One Six
    Three Two
    Three Four
    Three Six
    Five Two
    Five Four
    Five Six
    So as you can see I am making SOME progress. I am not sure why the \r works but it made it format correctly. Now I need to fix the spaces at the beginning (which seems to be a tab) and four spaces at the end.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    How about:
    PHP Code:
    <?php
        $firstList 
    preg_split('/[\r\n]+/'$_POST['listOne'], PREG_SPLIT_NO_EMPTY); 
        
    $secondList preg_split('/[\r\n]+/'$_POST['listTwo'], PREG_SPLIT_NO_EMPTY);
        echo 
    '<textarea name="BroadPhraseExact" id="BroadPhraseExact" cols="45" rows="15">';
        foreach (
    $firstList as $first) {
             foreach (
    $secondList as $second) {
                echo 
    "{$first} {$second}\r\n";
             }
        }
        echo 
    '</textarea>';
    By splitting by an \r, \n OR \r\n (using the preg_split above) you are splitting by a newline - different systems handle newlines differently so it's best to handle all.

    Other than that, the problem could have been indenting or something.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2001
    Location
    Kansas City, MO
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    How about:
    PHP Code:
    <?php
        $firstList 
    preg_split('/[\r\n]+/'$_POST['listOne'], PREG_SPLIT_NO_EMPTY); 
        
    $secondList preg_split('/[\r\n]+/'$_POST['listTwo'], PREG_SPLIT_NO_EMPTY);
        echo 
    '<textarea name="BroadPhraseExact" id="BroadPhraseExact" cols="45" rows="15">';
        foreach (
    $firstList as $first) {
             foreach (
    $secondList as $second) {
                echo 
    "{$first} {$second}\r\n";
             }
        }
        echo 
    '</textarea>';
    By splitting by an \r, \n OR \r\n (using the preg_split above) you are splitting by a newline - different systems handle newlines differently so it's best to handle all.

    Other than that, the problem could have been indenting or something.
    That removes the spaces but I get:

    Ford Escape
    Edge
    Explorer

    Instead of:

    Ford Escape
    Ford Edge
    Ford Explorer

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry about that, I misinterpreted preg_split's manual page for the PREG_SPLIT_NO_EMPTY constant.

    Here you go, tried and tested:
    PHP Code:
    <?php
    $firstList 
    preg_split("/[\r\n]+/"$_POST['listOne']); 
    $secondList preg_split("/[\r\n]+/"$_POST['listTwo']);
    echo 
    '<textarea name="BroadPhraseExact" id="BroadPhraseExact" cols="45" rows="15">';
    foreach (
    $firstList as $first) {
       foreach (
    $secondList as $second) {
          echo 
    "{$first} {$second}\r\n";
       }
    }
    echo 
    '</textarea>';
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2001
    Location
    Kansas City, MO
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, it is now working. Not sure what happened.

    Andy idea how to strip an empty line if someone hits <enter> at the end of enter a list of words?

    Thanks again for all your help!

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ok, what I'm assuming is happening is that there is a blank value in the first array, so how about adding:

    PHP Code:
    $listOne array_filter($listOne);
    $listTwo array_filter($listTwo); 
    After you've done the preg_split()ting
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2001
    Location
    Kansas City, MO
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arkinstall,

    I really appreciate your help. I have made more progress today then the last two weeks combined.

    I wish I could do something for you in return.

    Thanks again,
    Andy

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sure you can

    Stay around and active on the forums, and help out with people's questions that you can give good input to. Not only will you learn a lot from it (helping other people and having responses and suggestions by other posters will teach you more than any tutorial could) but you will be helping out others. This is a web community, after all.

    By doing that, you've done something in return
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Tags for this Thread

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
  •