jagguy Posted July 14, 2007 Share Posted July 14, 2007 I search this string for non-alpha numeric chars and get rid of them. $my2 = preg_replace("/[^a-zA-Z0-9s]/", "", $my); What i want to do is the same but allow the string to keep a '.' or a '_' (dot and underscore). eg my3_one.txt is a string i would allow but my3*one.txt is not allowed. How do i do this ? Quote Link to comment Share on other sites More sharing options...
Caesar Posted July 14, 2007 Share Posted July 14, 2007 <?php $newstr = preg_replace('/[^a-zA-Z0-9\.\_ ]+/i', '', $string); ?> Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 14, 2007 Share Posted July 14, 2007 Close. Characters are not interpretered AS IS and do not need to be escaped when within [ and ] <?php $newstr = preg_replace('/[^_a-zA-Z0-9.]+/i', '', $string); ?> Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 16, 2007 Author Share Posted July 16, 2007 This doesn't replace a '.' fullstop when places at the end of a string. What else doesn't this do? if i enter wewew. (the fullstop at the end still appears) $title=(str_replace(".", "", $title)); $title2 = preg_replace("/[^a-zA-Z0-9s]/", "", $title); Quote Link to comment Share on other sites More sharing options...
MemphiS Posted July 16, 2007 Share Posted July 16, 2007 <?php if (!ctype_alnum(str_replace(array(".","_"),"",$title)){ print("Invalid..."); }elseif (ctype_alnum(str_replace(array(".","_"),"",$title)){ ?> This Checks $title for letters and numbers. A . and _ are allowed within the $title. Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 16, 2007 Author Share Posted July 16, 2007 I don't want any '.' fullstops in my string. I want them deleted . This fails to do this why? $title2 = preg_replace("/[^a-zA-Z0-9s]/", "", $title); I dont fully understand what this does but is it possible to still use pregreplace? I only want numbers and letters and that is it, no other char allowed. ctype_alnum(str_replace(array(".","_"),"",$title) Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 16, 2007 Share Posted July 16, 2007 you said in your fisrst post you wanted to keep the full stops!!!!! try... $title2 = preg_replace("/[^a-zA-Z0-9\.]/", "", $title); Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 16, 2007 Author Share Posted July 16, 2007 This is a different q) than the first sorry. This still fails to work if you have a fullstop at the end of a string $title $title2 = preg_replace("/[^a-zA-Z0-9\.]/", "", $title); if (strcmp($title2,$title)>0) //wont detect the fullstop at the end of string { mysql_close($link); header( "Location: ". $myvar."/upmessage.php" ); exit; } Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 16, 2007 Share Posted July 16, 2007 Sorry didn't notice the hat $title2 = preg_replace("/[^a-zA-Z0-9]|[\.]/", "", $title); Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 16, 2007 Share Posted July 16, 2007 Please check my last post. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 16, 2007 Share Posted July 16, 2007 please re iterate what you want - its a clear as mud (well not that bad but lets get it clear). you want to remove any non-alphanumeric apart from '.' is that correct? Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 16, 2007 Share Posted July 16, 2007 <?php $string = "a string with random.bloody_charecters!!!"; $newstr = preg_replace('/[^_a-z0-9.]+/i', '', $string); echo $newstr; // outputs :astringwithrandom.blood_charecters ?> Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 19, 2007 Author Share Posted July 19, 2007 When i validate a name as in uploaded filename i can't remove all nonalphanumeric chars with this preg_replace (ex the . and _ char) I can't remove the quote ` symbol from $filename. I do the 2 lines below 1st . $filename= $_FILES['uploadedfile']['name']; $filename=str_replace(" ", "_", $filename) ; this fails to remove ` symbol $file2= preg_replace('/[^a-zA-Z0-9\.\_ ]+/i', '', $filename); this works but i don't want to use it. // $filename=str_replace("`", "", $filename) ; this fails if ( strpos($filename,"`")===true) I want to compare the original to see if there is any no alpha chars ex for . _ and no spaces. If there is say a ` or % then simply redirect but the strcmp seems to fail. if (strcmp($filename,$file2)!=0) { mysql_close($link); header( "Location: ". $myvar."/upfile.php" ); exit; } Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 19, 2007 Author Share Posted July 19, 2007 redo above Now i want to detect whether a non alphanumic char is present except for the . and _ char. and then redirect page. I cant do this as this fails redirect page ,if a string that has non alphanumic char is present like the ` char $filename= $_FILES['uploadedfile']['name']; $filename=str_replace(" ", "_", $filename) ; // $filename=str_replace("`", "", $filename) ; $file2= preg_replace('/[^a-zA-Z0-9\.\_ ]+/i', '', $filename); $file2= mysql_real_escape_string($filename); if (strcmp($filename,$file2)!=0) { //redirect Quote Link to comment Share on other sites More sharing options...
jagguy Posted July 19, 2007 Author Share Posted July 19, 2007 I just found a preg_match command Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted July 19, 2007 Share Posted July 19, 2007 but you want to REPLACE things - at least thats what your OP said anyway.... Quote Link to comment 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.