i have a variable with a string
let's say 100 lines, one url per line
i can't think out a easy way to get each of these urls separetely in order to insert them into a DB

infact they are separated by a new line character (or in some cases space)
so i just need a way to read the sub-strings between the new lines....

    I don't understand. Show an example. On one hand you're saying there's on URL per line and then you're saying it's separated by spaces.

    A simple thing is to use explode() to break it up. The example below assumes that you want to create one row per URL in your Database:

    $string = "http://www.example1.com
    http://www.example2.com
    http://www.example3.com";
    
    $url = explode("\n", $string);
    
    for ($i=0, $cnt = count($url); $i < $cnt; $i++) {
        // Here you can insert each url into the DB (separate rows)
        echo $url[$i];  // Displays one at a time
    }
    

      yeah, this will do the job, thanks! 🙂

        14 days later

        welll i tried it
        i'm entering around 100 urls in a <textarea> box
        and trying to output it for a test with this code

         
        if(isset($_POST['Save'])) 
        {
        import_request_variables("P"); 
        //echo $urls;
        $urls = explode("\n", $urls);
        var_dump("$urls");
        
        
        die();
        }
        

        but it outputs only "string(5) "Array" "
        wich according to the manual means that it haven't found the separator char in the string.....

        well what separator char i should use?

          Instead of the var_dump() use:

          echo '<pre>', print_r($urls, TRUE), '</pre>';

          to really get a good look at the structure of the array.

          .

            thanks but the problem is that infact explode() doesn't put anything in the array at all....

              Have you used my last bit of code anyway?

              Well, without seeing what the url data looks like, nobody can really help.

              You'll just have to find another delimiter. i.e. maybe it's \r\n

              Good luck.

                In the forum, you should post what the exact "something" is. Also, if you're using "id=", then the variable name you should be using is $id and not $urls.

                Given this: http://domain.com/alabala/tralala.php?id=something, then it would go something like so:

                if(isset($_POST['Save']))  
                { import_request_variables('PG');
                echo $id; $urls = explode("\n", $id); echo '<pre>', print_r($urls, TRUE), '</pre>';
                die(); }

                EDIT:

                I forgot to put the "G" in the import_request_variables() function. When you have stuff coming through the URL, they will be in $GET and not $POST.

                Anyway, it's not a good idea to post long URL data, especially with carriage returns/newlines in them. If you insist on doing it, then you should look into using urlencode() and urldecode() functions.

                  well the id variable is not used
                  it's just part of the text which i have to separete on lines and enter into DB

                    So what's in $urls before you explode it? Put the print_r() before the explode() to see. Given what explode() does, it's probably empty already - so it's not explode() that's at fault, but something earlier in the program.

                      well guys you will laugh at me
                      but the problem is that i was using print_r("$urls") instead of print_r($urls)
                      that was the problem...
                      damn, i hate when such stupid mistake costs me hours....

                        i forgot
                        many many thanks for the help 🙂

                          Write a Reply...