-4

So I am trying to include spaces into my search string i new to using Regex so I just need a bit o guidance:

if(preg_match("/^[A-Za-z]+(\s[A-Za-z]+)*$/", $_POST['name'])){

I know that the \s is meant to be providing that for me but I can only search strings with not characters?

if (preg_match("^(?i)[a-z\s]+$", $_POST['name'])) {    
    $name = $_POST['name'];

    $q = "SELECT *
        FROM users 
        WHERE first_name 
        LIKE '%" . $name .  "%' 
        OR last_name LIKE '%" . $name ."%' OR email LIKE '%" . $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>";         
    }
}
gevorg
  • 4,835
  • 4
  • 35
  • 52
PhpDude
  • 1,542
  • 2
  • 18
  • 33
  • 1
    What kinds of strings do you (not) want to match? – Rizier123 Jul 23 '15 at 20:13
  • I need lower an upper case, I need numeric and also the @ sign plus spaces but apart from that I do not require any special characters? – PhpDude Jul 23 '15 at 20:15
  • Try `/^[\d@a-z][\d@a-z ]*$/i` ;). – shA.t Jul 23 '15 at 20:16
  • @shA.t still seems to be the same, for example if I submit a name like 'Mr X' it gives no nothing but I know he is in the database, but if I type 'Mr' He shows up along with all the other 'Mr' entries – PhpDude Jul 23 '15 at 20:17
  • Why did I get a downvote....? – PhpDude Jul 23 '15 at 20:18
  • Is the string multi-lined? By `not characters` do you mean without `\s` characters? – chris85 Jul 23 '15 at 20:19
  • I think the issue maybe that in my database is has a column for first name and columns for last name so I guess the space makes no sense to the search - see my update – PhpDude Jul 23 '15 at 20:20
  • @Eder `Warning: preg_match(): No ending delimiter '^' found in ` – PhpDude Jul 23 '15 at 20:21
  • See manual on delimiters, http://php.net/manual/en/regexp.reference.delimiters.php (which you were using in your first code). You shouldn't pass user input direct to your query. – chris85 Jul 23 '15 at 20:24
  • I think your problem is in your query not in your regex, So can you give us some sample data in your table ;). – shA.t Jul 23 '15 at 20:26
  • @shA.t Hey checkout my question do you require more info to help? – PhpDude Jul 23 '15 at 20:26
  • What is wrong with `^(?i)[a-z\s]+$`? – chris85 Jul 23 '15 at 20:38
  • @chris85 I get this error `Warning: preg_match(): No ending delimiter '^' found in...` – PhpDude Jul 23 '15 at 20:39
  • Yes, and did you not see my comment and/or look at your previous regex? http://stackoverflow.com/questions/31597188/search-for-whitespaces-failing-regex#comment51146426_31597188 – chris85 Jul 23 '15 at 20:40
  • @chris85 I am very new to this so please excuse my naivety I have looked at the manual you sent me but I am struggling to understand what it is guiding me – PhpDude Jul 23 '15 at 20:41
  • BTW, you need to double escape `\s` in double quotes: `"/^[A-Za-z]+(\\s[A-Za-z]+)*$/"`. – Wiktor Stribiżew Jul 23 '15 at 20:42
  • @stribizhev is it because I am trying to search from two columns? – PhpDude Jul 23 '15 at 20:44
  • @phpcoder: No, just your regex might be incorrectly interpreted inside double quotes. To match whitespace only, you can declare a `"/\\s+/"` or `'/\s+/'` regex patterns. I do not know if it fixes anything, just a note. – Wiktor Stribiżew Jul 23 '15 at 20:46
  • Delimiters tell the regex engine where the expression starts and ends. They must be the same. They must be escaped if used in the expression. So you should use `/^(?i)[a-z\s]+$/`. – chris85 Jul 23 '15 at 20:46
  • @stribizhev Thank you for the example but still not getting a result – PhpDude Jul 23 '15 at 20:49
  • @chris85 Still no results I am afraid – PhpDude Jul 23 '15 at 20:49
  • Do an echo after the `preg_match`. See where you are failing. – chris85 Jul 23 '15 at 20:50
  • Anything, just for debugging `echo 'we are in conditional';`. – chris85 Jul 23 '15 at 20:51
  • @phpcoder: As mentioed by chris85, you must use delimiters. /^(?i)[a-z\\s]+$/ – Eder Jul 23 '15 at 20:51
  • @chris85 so by using the echo function it did spit out `echo 'we are in conditional';` – PhpDude Jul 23 '15 at 20:54
  • @Eder but it still gives me no result. I can get a result by searching EITHER the first or last names but not a a string with a space – PhpDude Jul 23 '15 at 20:55
  • It said `we are in conditional` or it said `echo 'we are in conditional';`?? If you got that then regex worked. – chris85 Jul 23 '15 at 20:55
  • I got `we are in conditional` – PhpDude Jul 23 '15 at 20:56
  • You are searching for `john smith` in the `like`s?? That won't work you have separate columns. Split on white spaces, this is a different question. – chris85 Jul 23 '15 at 20:56
  • @chris85 so from my above what is it that is going wrong sorry? – PhpDude Jul 23 '15 at 20:56
  • I'll write up what is happening.. – chris85 Jul 23 '15 at 20:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84120/discussion-between-phpcoder-and-chris85). – PhpDude Jul 23 '15 at 20:57

1 Answers1

1

So your current process the regex is correct your SQL is not. Lets say you have $name = 'john smith';. Put this into your query and see what happens.

SELECT *
    FROM users 
    WHERE first_name 
    LIKE '%john smith%' 
    OR last_name LIKE 'john smith%' OR email LIKE '%john smith%'

A firstname is not like john smith, a lastname is not like john smith, and an email is not like john smith.

A firstname might be like john and a lastname might be like smith. So maybe separate on the white space. So as you can see no matches are found

In any event the

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

is correct.

Update:

An adaption of this answer, MySQL select with CONCAT condition, I think would work be the best approach.

$q = "SELECT *
      FROM users 
      WHERE CONCAT(first_name, ' ', last_name)
      LIKE '%" . $name . "%' or email LIKE '%" . $name . "%'";
Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51
  • @RAnders00 Yea, OP could have used parameterized query to prevent that. Regex does elimintate that though. `^(?i)[a-z\s]+$`, e.g. Query only runs when `$name` only has letters and spaces. – chris85 Jun 02 '16 at 21:13