Jump to content

Check String for Chars


darkhappy

Recommended Posts

How can I check $var for characters 0 - 9 and a -z (and what ohter normal charactes should I look for?)?

 

 

I am processing a value from a CSV file if I run the check if ($var!='') on some of the fields it returns TRUE even though the CSV field is empty, so I need to check the $var to make sure it contains data. Please let me know if this question does not make sense so I can re-phrase or give more info.

 

 

 

Thank you,

dhappy

Link to comment
https://forums.phpfreaks.com/topic/106064-check-string-for-chars/
Share on other sites

Best solution for you would be using ctype_alnum(). It's more efficient than using a regular expression, and it does the job.

 

 

The regex way:

 

<?php

if(preg_match("/^[a-z0-9]*$/i", $str)) 
{
   //$str is valid
}

?>

 

 

Orio.

 

I have tried both ways and empty fields are still evaluating to true. What am I doing wrong?

 

 

Here is my code:

 

	<?php
foreach($checkarr1 as $v4) {
		if(ctype_alnum($v4)) {
			echo "'".$v4."'-";
		}
}
?>

 

 

$checkarr1 is an array created from a row in a CSV file, some values are null and I only want to echo $v4 for valid fields. I put '' around the var so I can see the blanks when it outputs.

 

 

 

thanks,

dhappy

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.