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 ? Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-297952 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); ?> Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-297999 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); Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299345 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. Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299348 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) Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299363 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); Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299387 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; } Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299429 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); Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299455 Share on other sites More sharing options...
drewbee Posted July 16, 2007 Share Posted July 16, 2007 Please check my last post. Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299467 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? Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299474 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 ?> Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-299539 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; } Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-302189 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 Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-302211 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 Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-302239 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.... Link to comment https://forums.phpfreaks.com/topic/59907-search-string-partially/#findComment-302307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.