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
Share on other sites

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
Share on other sites

<?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
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)

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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