Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts

    Highlight invalid characters in string

    Ok, I've been trying to figure this out for about 6 hours and everything I've tried just hasn't worked the way I want it. This is what I've got so far (had to use code tag as php tag kept stripping the backslashes)
    Code:
    // being called by validate("mem-em/_-e mssssss+em",username,3,12);
    
    
    function validate($field,$what,$minchar,$maxchar){
    
            if(eregi("^[_a-z0-9]{".$minchar.",".$maxchar."}$",$field)){
                $notice = 'Ok, validated fine';
            }else{
                if(strlen($field) < $minchar){
                    $notice = ucfirst($what).' must contain more than '.$minchar.' characters<br>';
                }
                if(strlen($field) > $maxchar){
                    $notice = ucfirst($what).' must be no more than '.$maxchar.' characters<br>'."\n";
                }
                $invalid = array("-","\+","\.","\\\\","\s");
    
                foreach($invalid as $no){   
                
                    if(preg_match("/".$no."/",$field)){
                        if($no == "\s"){
                            $no = "spaces";
                        }
                        stripslashes($no);
                    }
                    $errors .= stripslashes($no.' ');
                }
                $notice .= 'ERROR: '.ucfirst($what).' must not contain: '.$errors."<br>\n";
            }
            
            return $notice;
        }
    I'm wanting it to highlight all instances of $invalid in the $field. I've tried adding str_replace($invalid,"<b>".$invalid."</b>",$field) but that just returns as Array being highlighted. I've also tried str_replace and preg_replace in the foreach loop, but then it lists the $field with all the errors found too. I can't think of any other way to do it.

    Any ideas?
    Last edited by Nightfire; 01-10-2004 at 06:17 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's a possible solution:

    Code:
    $highlightedField = preg_replace(
    	'/('.implode($invalid, '+|').')/', 
    	'<span style="color:red;font-weight:bold">\\1</span>', 
    	$field
    );
    
    $notice .= 'ERROR: '.ucfirst($what).' must not contain: '.$errors."<br>\n";
    $notice .= 'you wrote: ' . $highlightedField;
    Note that I decided to add red color to the invalid chars, because a bold '-' is indistinguishable from normal text.
    De gustibus non est disputandum.

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Thanks alot, just what I was after. I knew you'd be the one to rescue me lol


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •