Grow your CSS skills. Land your dream job.

Exploding a list of urls…Going insane.

  • # December 6, 2011 at 2:48 pm

    Well, I am a front end guy, but I have an interest in everything so i am trying to figure out how to use PHP for some mundane tasks…basically what i want to do is split a list (delimited by a comma) and insert the values of the list into params/attribute in so i only have to copy paste a list once instead of the thousands of urls over and over…Here is where i got stuck:







    < ?php
    if (isset($_POST)) {
    $urls = trim($_POST);
    $arr = explode( ",", $urls);
    $count= count($arr);

    // need to loop through the array here...don't know how...i was thinking a for or while loop..??
    echo '' . thearrayitem[0] . '';
    }
    ?>

    // It should output something like:

    http://www.link1.com
    http://www.link2.com
    http://www.link3.com

    Except “thearrayitem[0]” should be the index as a variable so it can iterate through the items…
    Any help would be much appreciated thanks gin advance!

    # December 6, 2011 at 3:24 pm

    I’m not really a PHP guy either, but to loop through the array wouldn’t it be something like this:


    for ($i = 0; $i < = count(thearrayitem); $i++) {
    echo ' . '>' . thearrayitem . '';
    }
    # December 6, 2011 at 3:26 pm

    Yes, for some reason I just mixed up the syntax. Thanks a lot @johnnyb

    # December 6, 2011 at 4:18 pm

    Ok, still not working for some reason…

    this is what i have now…im getting an undefined offset error, but $i should be set….unless my loop is failing???







    < ?php
    if (isset($_POST)) {
    $urls = trim($_POST);
    $arr = explode( ",", $urls);
    $count = count($arr);
    for ($i = 0; $i < = $count; $i++) {
    echo '' . $arr[$i] . '';
    }
    }
    ?>
    # December 8, 2011 at 2:24 pm

    Here is the solution in case any other noobs run into the issue…

    Instead of trying to use the index, simply use a foreach loop and use the value like so:


    if (isset($_POST)) {
    $urls = trim($_POST);
    $arr = explode( ",", $urls);
    foreach ($arr as $link) {
    echo '' . $link . '';
    }
    }
    ?>

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".