php - Search for whitespaces failing Regex -


so trying include spaces search string new using regex need bit o guidance:

if(preg_match("/^[a-za-z]+(\s[a-za-z]+)*$/", $_post['name'])){ 

i know \s meant providing me can search strings not characters?

if (preg_match("^(?i)[a-z\s]+$", $_post['name'])) {         $name = $_post['name'];      $q = "select *         users          first_name          '%" . $name .  "%'          or last_name '%" . $name ."%' or email '%" . $name ."%'";      $r = mysqli_query($dbc, $q);      while($row = mysqli_fetch_array($r)) {           $firstname = $row['first_name'];           $lastname = $row['last_name'];           $email = $row['email'];           $extension = $row['extension'];           $id = $row['user_id'];            echo "<ul>\n";           echo "<li>" .$firstname . " " . $lastname .  " " . $email . " " . $extension . "</a></li>\n";           echo "</ul>";              } } 

so current process regex correct sql not. lets have $name = 'john smith';. put query , see happens.

select *     users      first_name      '%john smith%'      or last_name 'john smith%' or email '%john smith%' 

a firstname not john smith, lastname not john smith, , email not john smith.

a firstname might john , lastname might smith. maybe separate on white space. can see no matches found

in event the

if(preg_match('/^(?i)[a-z\s]+$/', $_post['name'])){ 

is correct.

update:

an adaption of answer, mysql select concat condition, think work best approach.

$q = "select *       users        concat(first_name, ' ', last_name)       '%" . $name . "%' or email '%" . $name . "%'"; 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -