rastaman46 Posted September 30, 2012 Share Posted September 30, 2012 Hello im here again im just strugling to add comma between Ip adresses im get result in array like this "141.237.52.41 86.1.223.181" but i need "141.237.52.41,86.1.223.181" Im tryd implode but no succes Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/ Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 Why would you do this? Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381903 Share on other sites More sharing options...
rastaman46 Posted September 30, 2012 Author Share Posted September 30, 2012 I need get users from sql by matching ip up where userip In ('141.237.52.41','86.1.223.181') Like this output im get im can do this Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381904 Share on other sites More sharing options...
spfoonnewb Posted September 30, 2012 Share Posted September 30, 2012 (edited) You can also use preg_replace('/\s/', '', $input) or explode() and many others. However, are you talking about an array or a string? because you mentioned an array but provided a string. Edited September 30, 2012 by spfoonnewb Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381906 Share on other sites More sharing options...
rastaman46 Posted September 30, 2012 Author Share Posted September 30, 2012 Yes its array foreach ($returnStats['listener'] as $listener) { $listeners['LISTENERS'] = array( 'HOSTNAME' => (string) $listener->HOSTNAME, 'USERAGENT' => (string) $listener->USERAGENT, 'CONNECTTIME' => (string) $listener->CONNECTTIME, 'POINTER' => (string) $listener->POINTER, 'UID' => (string) $listener->UID, ); echo $listeners['LISTENERS']['HOSTNAME']; } Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381907 Share on other sites More sharing options...
spfoonnewb Posted September 30, 2012 Share Posted September 30, 2012 So I assume your hostname is the IP? Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381908 Share on other sites More sharing options...
rastaman46 Posted September 30, 2012 Author Share Posted September 30, 2012 yes Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381909 Share on other sites More sharing options...
rastaman46 Posted September 30, 2012 Author Share Posted September 30, 2012 Im tryd use explode like this $hmm = $listeners['LISTENERS']['HOSTNAME']; $ip = explode(" " ,$hmm); echo $ip; output = array array Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381910 Share on other sites More sharing options...
Christian F. Posted September 30, 2012 Share Posted September 30, 2012 Where do you get the IP address you want to match from, and how does the source data look? You can find out by using var_dump (). Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381912 Share on other sites More sharing options...
rastaman46 Posted September 30, 2012 Author Share Posted September 30, 2012 (edited) source im taking it from is shoutcast DNSA server XML file var_bump return this string(13) "141.237.52.41" string(12) "86.1.223.181" Edited September 30, 2012 by rastaman46 Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381915 Share on other sites More sharing options...
spfoonnewb Posted September 30, 2012 Share Posted September 30, 2012 (edited) You can either build your own array to implode (use a key based recursive implode) or build the string yourself. Building the string yourself in this case is easiest assuming you needed that second listeners array rebuilt. Also, you should really validate that IP (make sure it's an IP in the correct format) and/or use prepared statements. <?php //Assume the dataSet appears like this based upon the provided foreach. $returnStats = array( 'listener' => array( (object) array( 'HOSTNAME' => '192.168.1.1', 'USERAGENT' => 'Google Chrome', 'CONNECTTIME' => '60', 'POINTER' => '0', 'UID' => '1Z5548488484' ), (object) array( 'HOSTNAME' => '192.168.1.2', 'USERAGENT' => 'Mozilla Firefox', 'CONNECTTIME' => '120', 'POINTER' => '0', 'UID' => '1Z5584844484' ) ) ); $listeners = array(); $ipStr = 'WHERE IN ('; foreach ($returnStats['listener'] as $listener) { $listeners[] = array( 'HOSTNAME' => (string) $listener->HOSTNAME, 'USERAGENT' => (string) $listener->USERAGENT, 'CONNECTTIME' => (string) $listener->CONNECTTIME, 'POINTER' => (string) $listener->POINTER, 'UID' => (string) $listener->UID ); $ipStr .= "'{$listener->HOSTNAME}',"; } $ipStr = substr($ipStr, 0, -1).')'; var_dump($ipStr); //You can use the listeners array if you need it, but you shouldn't define a key. var_dump($listeners); Edited September 30, 2012 by spfoonnewb Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381917 Share on other sites More sharing options...
rastaman46 Posted September 30, 2012 Author Share Posted September 30, 2012 Love u man Big thanks Quote Link to comment https://forums.phpfreaks.com/topic/268936-hellp-need-seperate-ip-with-comma/#findComment-1381918 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.