We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

remove character from string

Insoftservice
on
Medium Priority
421 Views
Last Modified: 2012-05-11
Hi,
I have as string with ip in such a format
1> 192.168.1.1
2> 192.168.1.1-192.168.1.255,80.89.67.3, 81.56.89.2
3> 192.168.1.1 - 192.168.1.5
4> 192.168.1.5,192.168.1.6

where
'-' => range,
',' => specific i/p.
so, my question was how to remove range ip and specific ip from the given string. say for eg string 2>
it has both range and specific i/p.
Expected result for 2>
range = 192.168.1.1-192.168.1.255
specific i/p [0] = 80.89.67.3
specific i/p [1] = 81.56.89.2
Comment
Watch Question

Loganathan NatarajanLAMP Developer
CERTIFIED EXPERT

Commented:
try like this,
<?php
$var2 = "192.168.1.1-192.168.1.255,80.89.67.3, 81.56.89.2";

$merges = preg_split("/[-\s,]+/", $var2);

//print_r ($merges);

$range = $merges[0].'-'.$merges[1];
echo "<br>range =".$range;

$specific1 =  $merges[2];
echo "<br> specific i/p [0] = ".$specific1;

$specific2 = $merges[3];
echo "<br> specific i/p [1] = ".$specific2;

?>

Open in new window

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi
thx all
@logudotcom if the filed increases it fails it gives hard coded data ie.
192.168.1.1-192.168.1.255,80.89.67.3,81.56.89.2,81.56.89.23,192.168.11.1-192.168.11.255
it would fail.
@yadaya: is near to the mark i am just doing slight work on it.

Author

Commented:
Actually i have to test whether the given ip in the string is matching with my desired ip address.
with range and specific ip validation
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thx @yadaya.

is it possible for ipv6 format too.

Author

Commented:


function ip2long6($ipv6) {
  $ip_n = inet_pton($ipv6);
  $bits = 15; // 16 x 8 bit = 128bit
  while ($bits >= 0) {
    $bin = sprintf("%08b",(ord($ip_n[$bits])));
    $ipv6long = $bin.$ipv6long;
    $bits--;
  }
  return gmp_strval(gmp_init($ipv6long,2),10);
}

will it work for 1pv6? any idea

Commented:
Read comments here http://php.net/manual/en/function.ip2long.php there is ip2long6 example function.

Author

Commented:
hi,
i am looking at that page, and wanted to confirm whether ist any issue in it or not

Author

Commented:
hi,
is there any way to get exact ip address of client
$_SERVER['REMOTE_ADDR'] is giving me 127.0.0.1

Author

Commented:
hi,
actually my website www.abc.com gets data from www.xyz.com after passing some parameter.
if executed  www.xyz.com  its gives correct address but when executed www.abc.com it gives 127.0.0.1 i.e ip of www.abc.com.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Actually i have to test whether the given ip in the string is matching with my desired ip address.
actually my website www.abc.com gets data from www.xyz.com after passing some parameter.

Any more secrets you want to tell us?

Couple of ideas... If xyz.com knows the true IP address of the client and abc.com only knows the IP address of xyz.com, why not let xyz.com return the IP address to abc.com?  It can be part of an XML or JSON string - easy!
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi,
Ya , yadaya had already given good code, no doubt even ur code would be helpful @ray.
Thanx for ur help
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.