Jump to content

detecting a character in a string.


mamanybono

Recommended Posts

Hi everybody,

 

I am trying to write a code which record a CIDR (5.2.80.0/21 5.11.128.0/17) format ip list into mysql database. I have a problem at one part of my code. Code is the below. With this code I am trying to find every third "." character's place and every "/" character's place and every " " characters place. After finding the places the code record the places in to an array which name is $reference_points. The problem is that for exaple if I apply this code on this 5.2.80.0/21 5.11.128.0/17 string. The result should be $reference_points.[1]=7 $reference_points.[2]=9 $reference_points.[3]=12 but it shows $reference_points.[1]=7 $reference_points.[2]=9 $reference_points.[3]=23. And the full list of CIDR ip is attached. By the way I use a form on php page to take this list. The code part is which start fith FOR loop. Thak you very much.

 

 

<div align="center">
<form action="add_new_country.php" method="post">
<table>
<tr>
<td>Country name:</td> <td><input type="text" maxlength="50" name="new_country_name" id="new_country_name" size="38" /></td></br></br>
</tr>
<tr>
<td>Country Ip Ranges:</td> <td><textarea rows="10" cols="30"  name="country_ip_ranges" id="country_ip_ranges"> </textarea></td></br></br>
</tr>
<tr>
<td><input type="submit"  name="submit" value="Add New Country" /></td><td></td>
</tr>
</table>
</form>
</div>



<div align="center">
<?php
if(isset($_POST["new_country_name"]) && isset($_POST["country_ip_ranges"]) && !empty($_POST["new_country_name"]) &&             !empty($_POST["country_ip_ranges"])){
$country_name=@$_POST['new_country_name'];
$country_ip_range=@$_POST['country_ip_ranges'];

$sql=mysql_query("SELECT id FROM countries WHERE  country_name='$country_name' LIMIT 1");
$exist_count=mysql_num_rows($sql);

if($exist_count==1){
echo "The name of the country which you try to add is already exist in the system, please try to add different name.";
}else{
mysql_query("INSERT INTO countries (id,country_name) VALUES ('','$country_name')" );
mkdir("$country_name");
$dot=0;
$reference_points=array();
$reference_points_index=1;

for($i=1;$i<=strlen(trim($country_ip_range));$i++){
if(substr(trim($country_ip_range),$i-1,1)=='.' && $dot<3){
$dot=$dot+1;

if($dot==3){
$reference_points[$reference_points_index]=$i;
$dot=0;
$reference_points_index=$reference_points_index+1;
}

}elseif(substr(trim($country_ip_range),$i-1,1)=='/'){
$reference_points[$reference_points_index]=$i;
$reference_points_index=$reference_points_index+1;

}elseif(substr(trim($country_ip_range),$i-1,1)==' '){
$reference_points[$reference_points_index]=$i;
$reference_points_index=$reference_points_index+1;
}
}
echo $reference_points[1].'<br/>';
echo $reference_points[2].'<br/>';
echo $reference_points[3].'<br/>';
echo $reference_points[4].'<br/>';
echo $reference_points[5].'<br/>';
echo $reference_points[6].'<br/>';
echo $reference_points[7].'<br/>';
echo $reference_points[8].'<br/>';
}
}
?>
</div>

19023_.txt

Link to comment
Share on other sites

Where are you getting $reference_points.[2]=9

 

There's no 9 in 5.2.80.0/21 5.11.128.0/17

 

You probably want this

 

<?php

$string = '5.2.80.0/21 5.11.128.0/17';

$pattern = '%(\d+)\.(\d+)\.(\d+)\.(\d+)/(\d+)%';
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);

var_dump($matches);

?>

 

Output

 

array (size=2)
  0 => 
    array (size=6)
      0 => string '5.2.80.0/21' (length=11)
      1 => string '5' (length=1)
      2 => string '2' (length=1)
      3 => string '80' (length=2)
      4 => string '0' (length=1)
      5 => string '21' (length=2)
  1 => 
    array (size=6)
      0 => string '5.11.128.0/17' (length=13)
      1 => string '5' (length=1)
      2 => string '11' (length=2)
      3 => string '128' (length=3)
      4 => string '0' (length=1)
      5 => string '17' (length=2)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.