Jump to content
Sign in to follow this  
Balderick

how to use preg_replace

Recommended Posts

I want to use preg_replace to input only a-zAZ with a limit of 30 characters

 

I have a piece of code I used before but it works with preg_match.

 

example function:

     <?php 
      // input form to get the name
     // use the function here
      $name = valid_name($name);


      // function to validate name input  
     function valid_name($data){

	$data = ltrim($data);
    $data = rtrim($data);

	$data = preg_replace("/^(?:[A-Za-z][A-Za-z\-]{2,30}[A-Za-z]|[A-Za-z])$/", '' , $data);
	
    return $data;
     }
    ?>

To give an impression of how I use it; a part of the confirmation form

    <html>
    <form action="mypage.php" method="post">
    <p><input type="text" name="name" value="<?php echo $name; ?>"         STYLE="background-color: #708DCC; color: white; height: 20px; width: 200px; border: none; font-size: 14px;" readonly ></p>
    
     <!-- rest of the form     -->
    </html>

The problem is in the preg_replace part. an input with chars like: ^&$ etc is not replaced, how to do this?

 

if anyone can help I would be really happy.

Share this post


Link to post
Share on other sites

That's not actually validation, that's sanitation. Validation would be if you told the user that the name they posted is invalid (which would probably be a better idea, since they might not like the outcome of the replacement your code is making).

 

Your current preg_replace() call will actually replace an entire valid string with nothing.

 

Here's how you would remove unwanted characters:

$name = preg_replace('[^a-zA-Z]', '', $name);

This code removes anything that's not a letter.

Share this post


Link to post
Share on other sites

That's not actually validation, that's sanitation. Validation would be if you told the user that the name they posted is invalid (which would probably be a better idea, since they might not like the outcome of the replacement your code is making).

 

Your current preg_replace() call will actually replace an entire valid string with nothing.

 

Here's how you would remove unwanted characters:

$name = preg_replace('[^a-zA-Z]', '', $name);

This code removes anything that's not a letter.

 

 

I had to change the code.

 

I added another delimiter, namly the foreslash /. Can the [ ] be considered as a delimiter? This would mean 2 delimiters are required

 

anyway here's my final solution:

    $name = preg_replace('/[^a-zA-Z]/', '', $name);

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...