Jump to content

search string partially


jagguy

Recommended Posts

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

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)

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;

 

  }

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;

}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.