SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast mikeall's Avatar
    Join Date
    Sep 2003
    Location
    Puerto Escondido, Mexico
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question help with generating a simple list of urls

    After much trial and error, reading and searching, I can't seem to work out how to do this. If you can help, your advise & tips will be greatly appreciated!

    I have php code that accesses a file that contains a list like this:

    url1.com : 1 Linked Text ; Short description of the url1
    ~
    url2.com : 2 Linked Text ; Short description of the url2
    ~
    url3.com : 3 Linked Text ; Short description of the url3

    Here is the code

    <?php
    $list = join ('', file ('list.php'));
    $content = split("~",$list);
    $rows = str_replace(array(' : ',' ; '),'<br>',$content);
    #$rows = array_slice($rows, 0, 10); // limits the list
    echo "<ol>";
    foreach ($rows as $row)
    {
    echo "<li>";
    echo ($row); // displays the content without html
    // the line below only shows 1 character not the full word
    #echo "<a href=".$row[0].">".$row[1]."</a> - ".$row[2];
    echo "</li>";
    }
    echo "</ol>";
    ?>

    I am wanting the output to have a row somewhat like this:
    <a href="url1">1 Linked Text</a> Short description for the url1

    Any ideas what I am doing wrong?

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all you have wrong file format and as a result - funny code to read it
    Line break itself is good record separator. And widely used one. PHP even has a function to read such a format. a file() function.
    So, make your file as this:
    Code:
    url1.com : 1 Linked Text ; Short description of the url1
    url2.com : 2 Linked Text ; Short description of the url2
    url3.com : 3 Linked Text ; Short description of the url3
    name it list.txt, not list.php,
    and then use just this code to get it into array:
    PHP Code:
    $content file ('list.txt')); 
    Now, note that $content is an array. You can't use any string function on an array.

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    $rows is an array of strings. So in the foreach $row is a string.
    $row[0] will be the first character, $row[1] the second, $row[2] the third
    *in some the character is whitespace i.e. newline

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'true);

    $test_str "url1.com : 1 Linked Text ; Short description of the url1
    ~
    url2.com : 2 Linked Text ; Short description of the url2
    ~
    url3.com : 3 Linked Text ; Short description of the url3"
    ;

    $content split("~",$test_str);
    $rows str_replace(array(' : ',' ; '),'<br>',$content);
    #$rows = array_slice($rows, 0, 10); // limits the list

    var_dump($rows);

    echo 
    "<ol>\r\n";
    foreach (
    $rows as $row)
    {
        echo 
    "<li>\r\n";
        echo (
    $row); // displays the content without html
        // the line below only shows 1 character not the full word
        
    echo "<a href=".$row[0].">".$row[1]."</a> - ".$row[2] . "\r\n";
        echo 
    "</li>\r\n";
    }
    echo 
    "</ol>";
    ?>
    It's easier to see whats going on if you look at the view-source.

  4. #4
    SitePoint Enthusiast mikeall's Avatar
    Join Date
    Sep 2003
    Location
    Puerto Escondido, Mexico
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the help so far. I have implemented the change to a txt file and now simple use the line break not the ~ which works fine.

    Since I am wanting the output to have a row somewhat like this:
    <a href="url1">1 Linked Text</a> Short description for the url1

    do I need it to be read into an array or string so that I can use the different parts in different orders per row?
    ie. Each row will have 3 parts I can and want to call: the url, the linked text, and the description.

    Any others tips or code of how to achieve this?

  5. #5
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    loop over your array, getting lines one-by-one and split them inside of loop, not before.

  6. #6
    SitePoint Enthusiast mikeall's Avatar
    Join Date
    Sep 2003
    Location
    Puerto Escondido, Mexico
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    loop over your array, getting lines one-by-one and split them inside of loop, not before.
    Ok I do understand what you sat but do not know how to do that. What goes where?

    Also, so am I needing to split the string into an array with 3 areas that then have keys which I can call?

    So now I have this code:
    Code:
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    
    $list = join ('', file ('list.txt'));
    $content = split("\n",$list);
    #$rows = str_replace(array(' : ',' ; '),'<br>',$content);
    $rows = $content;
    $rows = array_slice($rows, 0, 10); // limits the list
    echo "<ol>";
    foreach ($rows as $row) 
    { 
    #$row = str_replace(array(' : ',' ; '),'<br>',$row);
    $row0 = str_replace(' : ','/',$row);
    $row1 = str_replace(' : ','<br> row1 ',$row0);
    $row2 = str_replace(' ; ','<br> row 2 ',$row1);
    echo "<li>";
    #echo ($row); // displays the content without html
    // the line below only shows 1 character not the full word
    echo "<a href=".$row0.">".$row1."</a> - ".$row2;
    echo "</li>"; 
    } 
    echo "</ol>";
    ?>

  7. #7
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how to do what? to move a few lines of code in your script? Use copy and paste feature of your text editor.

    $list = join ('', file ('list.txt'));
    $content = split("\n",$list);
    I can't believe it

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $list = join ('', file ('list.php'));

    $content = split("~",$list);
    $rows = str_replace(array(' : ',' ; '),':',$content);
    echo "<ol>";
    foreach ($rows as $val)
    {
    $row=split(":",$val);
    echo "<li>";
    echo "<a href=".$row[0].">".$row[1]."</a> - ".$row[2];
    echo "</li>";
    }
    echo "</ol>";
    ?>

    it's so easy,just do this~~~

  9. #9
    SitePoint Enthusiast mikeall's Avatar
    Join Date
    Sep 2003
    Location
    Puerto Escondido, Mexico
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by Shrapnel_N5 View Post
    how to do what? to move a few lines of code in your script? Use copy and paste feature of your text editor.


    I can't believe it
    Thanks for your help. I must not have explained myself well. I do know how to copy and paste. What I didn't now was what code to write where.

    This is what I now have and it works but only shows 1 entry not the 3 or more.

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'true);

    $list join (''file ('list.txt'));

    $content split("/n",$list);
    $rows str_replace(array(' : ',' ; '),':',$content);
    echo 
    "<ol>";
    foreach (
    $rows as $val

    $row=split(":",$val);
    echo 
    "<li>";
    echo 
    "<a href=".$row[0].">".$row[1]."</a> - ".$row[2];
    echo 
    "</li>"

    echo 
    "</ol>";
    ?>
    So this is what the html should look like once I get this code correct when it gets the list info from the txt file which has:

    url0.com : 0 Linked Text ; Short description of the url0
    url1.com : 1 Linked Text ; Short description of the url1
    url2.com : 2 Linked Text ; Short description of the url2

    HTML Code:
    <ol>
    <li><a href=url0.com>0 Linked Text</a> - Short description of the url0</li>
    <li><a href=url1.com>1 Linked Text</a> - Short description of the url1</li>
    <li><a href=url2.com>2 Linked Text</a> - Short description of the url2</li>
    </ol>

  10. #10
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I am being dumb again.
    Take my apologies please.
    I thought str_replace don't work with arrays.

    As for your code, there is double mistake:
    1. A new line character in your split() is represented by \n sequence, not /n
    2. no split() needed at all. file() already does the job.
    So, make it just
    $content = file ('list.txt'));
    without join and split

  11. #11
    SitePoint Enthusiast mikeall's Avatar
    Join Date
    Sep 2003
    Location
    Puerto Escondido, Mexico
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! This is how it looks:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors'true);

    $content file ('list.txt');
    $rows str_replace(array(' : ',' ; '),':',$content);
    $rows array_slice($rows010); // limits the list
    shuffle ($rows); // randomises the rows
    echo "<ol>";
    foreach (
    $rows as $val

    $row=split(":",$val);
    echo 
    "<li>";
    echo 
    "<a href=".$row[0].">".$row[1]."</a> - ".$row[2];
    echo 
    "</li>"

    echo 
    "</ol>";
    ?>
    Thank you very much!


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
  •