Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

remove character from string

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
0
Insoftservice
Asked:
Insoftservice
  • 8
  • 3
  • 2
  • +1
3 Solutions
 
Loganathan NatarajanLAMP DeveloperCommented:
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

0
 
yadayaCommented:
<?php
        $input = "192.168.1.1-192.168.1.255,80.89.67.3,81.56.89.2";
        $range = array();
        $s_ip = array();
        $values = explode(",", $input);
        foreach ($values as $value)
        {
                if (count(explode("-", $value)) == 2 )
                     $range[] = $value;
                else
                     $s_ip[] = $value;
        }

        // Test
        print_r($range);
        print_r($s_ip);
?>

Open in new window

0
 
InsoftserviceAuthor 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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
InsoftserviceAuthor 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
0
 
yadayaCommented:
<?php
        $input = "192.168.1.1-192.168.1.255,80.89.67.3,81.56.89.2";
        function getAllowed($input)
        {
                $range = array();
                $s_ip = array();
                $values = explode(",", $input);
                foreach ($values as $value)
                {
                        if (count(explode("-", $value)) == 2 )
                             $range[] = $value;
                        else
                             $s_ip[] = $value;
                }
                $result['range'] = $range;
                $result['ip'] = $s_ip;
                return $result;
        }

        $allowed = getAllowed($input);

        $ip_to_chk = "192.168.1.35"; //IP FOR TESTING

        $is_in_range = 0; //

        foreach ($allowed[range] as $tmprange)
        {
                $tmprange=explode("-",$tmprange);
                if(ip2long($tmprange[0])<=ip2long($ip_to_chk) && ip2long($tmprange[1])>=ip2long($ip_to_chk))
                   $is_in_range=1;
        }
        foreach ($allowed[ip] as $tmpip)
        {
                if (ip2long($tmpip) == ip2long($ip_to_chk))
                        $is_in_range=1;
        }
        echo $is_in_range;
?>

Open in new window

0
 
InsoftserviceAuthor Commented:
thx @yadaya.

is it possible for ipv6 format too.
0
 
InsoftserviceAuthor 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
0
 
yadayaCommented:
Read comments here http://php.net/manual/en/function.ip2long.php there is ip2long6 example function.

0
 
InsoftserviceAuthor Commented:
hi,
i am looking at that page, and wanted to confirm whether ist any issue in it or not
0
 
InsoftserviceAuthor Commented:
hi,
is there any way to get exact ip address of client
$_SERVER['REMOTE_ADDR'] is giving me 127.0.0.1
0
 
InsoftserviceAuthor 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.
0
 
Ray PaseurCommented:
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!
0
 
Ray PaseurCommented:
Is this helpful?

http://www.laprbass.com/RAY_temp_insoftservice.php
Outputs:
192.168.1.5
192.168.1.6
RANGE:192.168.1.1-192.168.1.5
RANGE:192.168.1.1-192.168.1.255
80.89.67.3
81.56.89.2
192.168.1.1
<?php // RAY_temp_insoftservice.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE
$ipa = array
( '192.168.1.1'
, '192.168.1.1-192.168.1.255,80.89.67.3, 81.56.89.2'
, '192.168.1.1 - 192.168.1.5'
, '192.168.1.5,192.168.1.6'
)
;

// ITERATE OVER THE DATA STRINGS TO ISOLATE THE INDIVIDUAL COMPONENT IP ADDRESSES
$new = array();
foreach ($ipa as $str)
{
    // REMOVE ANY EXTRANEOUS BLANKS
    $str = str_replace(' ', NULL, $str);

    // IF THERE IS A COMMA IN THE STRING THERE ARE MULTIPLE UNIQUE IP ADDRESSES
    $arr = explode(',', $str);
    $new = array_merge($arr, $new);
}

// SHOW THE NEW ARRAY OF ADDRESSES
foreach ($new as $str)
{
    echo PHP_EOL;

    // IF THERE IS A HYPHEN IN THE STRING IT IS A RANGE OF IP ADDRESSES
    if (strpos($str, '-'))
    {
        echo "RANGE:";
    }
    echo $str;
}

Open in new window

0
 
InsoftserviceAuthor Commented:
Hi,
Ya , yadaya had already given good code, no doubt even ur code would be helpful @ray.
Thanx for ur help
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now