[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

php checking chars help

Does anyone have a function to check for spaces and to make sure length is not over 20 chars?
0
casit
Asked:
casit
  • 2
1 Solution
 
GawaiCommented:

<?php
function wordcount($str) {  
  return count(explode(" ",$str));
}  

// Example Usage:
$test = "This String has five words.";
echo "There are " . wordcount($test) . " in the sentence";
?>
0
 
casitAuthor Commented:
@gawai  Not what I was lookin for.  I was looking for something that checked a string to see if it had spaces OR if it was over 20 characters.
0
 
tkalchevCommented:
Use strlen, it gives you the string length in characters. Here is your example :

function has_space ( $str ) {
  $space_pos = strpos ( $str, " " );
  return $space_pos!==false;
}

function check_string ( $str ) {
  $length = strlen ( $str );
  $space = has_space ( $str );
  return ( $length<=20 ) &&  ! $space;;
}


0
 
casitAuthor Commented:
Thanks
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now