expression check problem

Discussion in 'PHP' started by dizyn, Dec 1, 2007.

  1. #1
    I want to validate:

    NEW Sony's 3.0" L.C.D. - 12MP DIGITAL CAMERA/VIDEO/TOUCH SENSOR K38

    Here is my expression:
    
    $regex='/^[a-zA-Z0-9_\.\-\;\:\'\"\/ ]{5,95}$/';
    
    PHP:

    Please help
     
    dizyn, Dec 1, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    It's working.
     
    nico_swd, Dec 1, 2007 IP
  3. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    you tired with:

    preg_match?
     
    dizyn, Dec 1, 2007 IP
  4. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    problem is that my expression is not working for:

    ', ", /. \


    any idea?

    thank you
     
    dizyn, Dec 1, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Yes, I tried this and it's working:
    
    $regex='/^[a-zA-Z0-9_\.\-\;\:\'\"\/ ]{5,95}$/';
    
    echo preg_match($regex, 'NEW Sony\'s 3.0" L.C.D. - 12MP DIGITAL CAMERA/VIDEO/TOUCH SENSOR K38');
    
    PHP:
    However, you don't have to escape all these characters. This is the same:
    
    $regex = '/^[\w-\.;:\'"\/ ]{5,95}$/';
    
    PHP:

    Both work for me. (tested)
     
    nico_swd, Dec 1, 2007 IP
  6. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Well it works file but if if is post via form i gets problem. any solution to that, I also tired: htmlspecialchars and addslashes

    Here is my code:


    
    	$regex='/^[a-zA-Z0-9_\.\-\;\:\'\"\/ ]{5,95}$/';
    	echo htmlspecialchars('NEW Sony\'s 3.0" L.C.D. - 12MP DIGITAL CAMERA/VIDEO/TOUCH SENSOR K38');exit;
    	$txt = htmlspecialchars($_POST["textfield"]);
    	echo $txt."<BR>";
    	echo preg_match($regex, $txt);	
    
    PHP:
    
    <form id="form1" name="form1" method="post" action="test1.php">
      <input name="textfield" type="text" value="<?PHP echo $_POST["textfield"]; ?>" size="75" />
      <input type="submit" name="Submit" value="Submit" />
    </form>
    
    Code (markup):
     
    dizyn, Dec 2, 2007 IP