Jump to content

Search for certain values in PHP string


johnsmith153

Recommended Posts

I have a PHP string which was created from a JavaScript text editor in a browser.

 

If they enter nothing the string is empty. If they enter lots of text, I will obviously see this in the PHP string.

 

However, if they enter just line breaks or press the enter button and no text then I will get something like:

$val = "<p> </p><br />" etc...

 

How can I search to see if a string only contains:

<p>

 

</p>

\r

\n

<br/>

 

If it ONLY contains these I can then just store a blank value in the database and not all the <p> and <br /> tags etc.

Link to comment
https://forums.phpfreaks.com/topic/207152-search-for-certain-values-in-php-string/
Share on other sites

You should never allow HTML to be input directly from a user. (htmlentities($input) to get rid of any html/javascript)

 

What you would do, is check whether there are any valid characters (a-z, 0-9), you can do this with a very simple preg match:

if(!preg_match("/[a-z0-9]+/i",$input)){ // This very simple preg checks whether there is at least 1 or more letters, or numbers (a Single number/character would pass this check).
   echo("Error, please enter valid data");
}

 

You can use much more complex queries to further sanitize your inputs.

 

-cb-

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.