I am trying to validate 3 input fields before they are submitted. Here is my php proccessform :

PHP Code:
<?
$profiles = Array();
function start_element($parser, $name, $attrs){
    global $profiles;
    if($name == "profile"){
        array_push($profiles, $attrs);
    }
}
function end_element ($parser, $name){}
$playlist_string = file_get_contents("servers.xml");
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse($parser, $playlist_string) or die("Error parsing XML document.");
print "<br />";
if($_POST['action'] == "ins"){
    array_push($profiles, Array(
                "ip" => $_POST['ip'],
                "server" => $_POST['server'],
                "country" => $_POST['country']));
    $profiles_final = $profiles;
}else if($_POST['action'] == "del"){
    $profiles_final = Array();
    foreach($profiles as $profile){
        if($profile['ip'] != $_POST['ip']){
            array_push($profiles_final, $profile);
        }
    }
}
$write_string = "<users>";
foreach($profiles_final as $profile){
    $write_string .= "<profile ip=\"$profile[ip]\" server=\"$profile[server]\" country=\"$profile[country]\" />";
}
$write_string .= "</users>";
$fp = fopen("servers.xml", "w+");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
print "<em>Server inserted or deleted successfully :)</em><br />";
print "<a href=\"serverlist.php\" ip=\"return\">Return</a>";
?>
Need to check that no special characters or just 0-9 a-Z and a few other characters like "." - "-" can be added . Becoz of the inserted data writes those 3 inputs to xml and when the xml gets writen with any special character it gives error .

Help whould be great , thank you .