Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP explode help

  • 26-05-2009 3:18pm
    #1
    Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭


    doing some screen scraping so i have a table of values.
    Table is split into rows and put into variables.

    to do so ive had to use something like ${'var'.$i } to increment the variable name
    [PHP] ${'var'.$i } = $data[$i] ;[/PHP]

    so
    var1= "a b c d e f g h i j k l";
    :
    :
    etc


    now i wanna use explode, so i can get rid of spaces and have values in an array.
    I.e
    var1[0] = a
    var2[1] = b

    but something like
    [PHP]${'pic'.$i } = explode(" ", ${'var'.$i });[/PHP]
    doesnt work


Comments

  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Placebo wrote: »
    doing some screen scraping so i have a table of values.
    Table is split into rows and put into variables.

    to do so ive had to use something like ${'var'.$i } to increment the variable name
    [PHP] ${'var'.$i } = $data[$i] ;[/PHP]

    so
    var1= "a b c d e f g h i j k l";
    :
    :
    etc


    now i wanna use explode, so i can get rid of spaces and have values in an array.
    I.e
    var1[0] = a
    var2[1] = b

    but something like
    [PHP]${'pic'.$i } = explode(" ", ${'var'.$i });[/PHP]
    doesnt work

    Are you trying to put "a b c d" into ONE array or different arrays ?

    You're setting var1[0] to "a", and var2[1] to "b" ?

    What are you setting var2[0] to ?

    And what value does $i have ?

    Try

    echo $i. " : ".explode(" ", ${'var'.$i });

    to see what PHP sees, before continuing on.

    And if that doesn't work, try a simple

    echo explode(" ", $var1);


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    i just checked,
    and basically it works when i explicitly declare an array but not when the programs fills it in, so im assuming there is something wrong with my filler code

    [PHP]<?php
    function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }

    $HTML = file_get_contents_curl("http://localhost:8080/recommend_pos.htm");


    $dom = new DOMDocument();
    $dom->preserveWhiteSpace = TRUE;
    @$dom-&gt;loadHTML($HTML);

    $xpath = new DOMXPath($dom);


    //Long

    $nodes = $xpath->query('/html/body/table[8]');


    $i = 0;

    foreach ($nodes as $node) {


    $cols = $node->getElementsByTagName('tr');

    foreach($cols as $cole)
    {
    $data2[$i] = $cols->item($i)->nodeValue;
    // $data[$i] = str_replace("Â", "", $data2[$i]);
    // $data3[$i] = str_replace(" ", "1", $data[$i]);
    //${'var'.$i } = $data[$i];



    $i++;

    }
    }

    ?>
    [/PHP]

    just tested filler by printing out
    echo $data2[1] outside of the loop and it seems fine


    //explodes fine with anything but the space, assuming its to do with dom/array


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Are you sure it's an actual space, and not an   or something else ?

    Is there a way that you can check ?


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    i kinda tested it out and seemed to be extra white spaces, so i used a regular expression to kinda get rid of it and subsitute space with # for easier explosion, this gonna slow down the script slightly so im gonna have to revisit the dom part again

    [PHP] ${'var'.$i} = $cols->item($i)->nodeValue;
    ${'vars'.$i} = str_replace("Â", "", ${'var'.$i} );
    ${'varx'.$i} = preg_replace('/\s\s+/', '#', ${'vars'.$i});

    ${'pic'.$i} = explode("#", ${'varx'.$i});[/PHP]

    thanks for your help,
    any dom/php experts - advice appreciated


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    If the reg exp works, just use the reg ex as the explode "needle".

    http://ie2.php.net/split


  • Advertisement
Advertisement