Strings and getting data

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:

f2fcd8a834.png


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:
'1,198.15.73.69\n1,198.15.73.70'

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.
 
Something like this?

PHP:
$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
 
Assuming that "\n" is always the new line character (other systems may use different line endings...) you could do this:

PHP:
$string = '1,198.15.73.69\n1,198.15.73.70';
$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 :)
 
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:

PHP:
            $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();

foreach($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

            $ip = ""; // TODO: GET THE IP ADDRESS
 
Last edited:
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:

PHP:
if(($key = array_search($ip, $lines)) !== false) {
    unset($lines[$key]);
}

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.
 
PHP:
$haxmple = '1,198.15.73.69' . "\n" . '1,198.15.73.70';
$haxmple = ltrim(rtrim($haxmple, "\n"), "\n");
$myStuff = array();
echo '<pre>';
$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]
        );
        $i++;
    }
}

print_r($myStuff);
// hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
couldn't resist from adding stupid comment, sorry
 
Top Bottom