1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Strings and getting data

Discussion in 'General PHP and MySQL Discussions' started by Robust, Jun 23, 2015.

  1. Robust

    Robust Well-Known Member

    I have the string: "1,123.456,567,567\n1,412.432.342.234\n1,314,3245,435,456\n2,324,435,246,324"

    So that data corresponds to this:


    The first part (before the comma) is the daemon ID. We can have lots of these, so relying on it just being 1 character isn't safe. We have more. So you first have the daemon ID, followed by a comma and then the IP address. After the IP address you just have another one on a new line.

    A database dump of this information:

    This is stored a string (yeah... I know...)

    So what I want to do is start the string AFTER the first comma and end it BEFORE the \ in "\n"

    After doing this I want to remove the entire part (so the daemon ID, the IP and the \n) from the table of available IPs and move it into a table of not available IPs, so as well as just the IP I also need to get the entire line.

    What I'm thinking is using a string manipulation function to get the entire line first (the first IP which starts with the daemon ID, daemon ID is in $daemonId). Then getting just the IP from that. Then running an update statement with the new text with the IP line removed from it.
  2. Daniel Hood

    Daniel Hood Well-Known Member

    Something like this?

    $string '';
    $lines explode("\n"$string); // splits into individual lines
    $data = array(); // array to hold the final data ([daemon id] => [ip])
    // loops through the lines
    foreach ($lines as $line)
    $split explode(','$line); // splits the daemon id from the ip
    $data[$split[0]] = $split[1]; // saves data[dameonid] = ip;
    var_dump($data); // dump the results
    Robust and Chris D like this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Assuming that "\n" is always the new line character (other systems may use different line endings...) you could do this:

    $string '1,\n1,';
    $array explode("\n"$string);

    foreach (
    $array AS $data)
        list (
    $daemonId$ip) = explode(','$data);
    Heh, Daniel Hood beat me to it. But yeah, basically the same thing :)
    Robust and Daniel Hood like this.
  4. Daniel Hood

    Daniel Hood Well-Known Member

    I even went through and commented it out.. step your game up. :p
    Robust and Chris D like this.
  5. Robust

    Robust Well-Known Member

    Got it. I'll test it out soon enough. It's a WHMCS hook by the way. Gotta love WHMCS, all their source is encrypted with ionCube so you're relying on a relatively good but poor documentation (good in some aspects, but really poor in others, very basic). Lucky that WHMCS does none of the work - all of the work is done by API requests here. Externally to the daemon handler as well as using WHMCS' internal API. Thanks @Daniel Hood @Chris D :)

    The code I ended up using was:

                $result select_query('tblservers''assignedips', array("serverhostname" => $vars['params']['serverhostname'])); // TODO: DON'T RELY ON SERVER HOSTNAME, USE SERVERID
    $array mysql_fetch_array($result); // COULD JUST DO mysql_fetch_array($result)['assignedips']
    $ipaddresses $array['assignedips'];
    $lines explode("\n"$ipaddresses);
    $data = array();

    $lines AS $line)
    $split explode(','$line);
    $data[$split[0]] = $split['1'];
    // the formatting is better in the actual code, pasting here just pasted it in one big line

    Last edited: Jun 23, 2015
  6. Robust

    Robust Well-Known Member

    Hm, so in this case the values in the $data array obviously don't have unique values. There will be lots of entries for daemon 1. Can I just do:

    $ip = $data[$daemonId];

    and it'll get the first result or any one result with that daemon ID. Then I need to construct the string again from the array, so reversing the steps:

    if(($key array_search($ip$lines)) !== false) {
    That would remove the value from the array, yeah?

    Then an implode:
    $string = implode(",", $array);

    Now I'm a little lost.

    Edit: I could implode again but it's already a string, so that's why I'm not sure what to do. I need to get the daemon IDs back into it to make a MySQL update query so it doesn't have the IP in the list so it can't be used again.
  7. batpool52!

    batpool52! Well-Known Member

    $haxmple '1,' "\n" '1,';
    $haxmple ltrim(rtrim($haxmple"\n"), "\n");
    $myStuff = array();
    $i 0// yo whatsup?

    foreach (explode(PHP_EOL$haxmple) as $line)
    $data explode(','$line);
        if (isset(
    $data[0], $data[1]))
    $myStuff[$i] = array(
    'daemon' => $data[0],
    'ip' => $data[1]

    // hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
    couldn't resist from adding stupid comment, sorry

Share This Page