Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Help explaining a Function

Tags:

sql

php

 function sql_like_expression($expression)
 {
     $expression = utf8_str_replace(array('_', '%'), array("\_", "\%"), $expression);
     $expression = utf8_str_replace(array(chr(0) . "\_", chr(0) . "\%"), array('_', '%'), $expression);

     return $this->_sql_like_expression('LIKE \'' . $this->sql_escape($expression) . '\'');
 }

I am not sure what this function is doing. From what I am seeing, i think it replaces _ with \_ though I am unsure if thats exactly whats going on. Why is it array's and what do the % mean? Cheers.

like image 987
John Riselvato Avatar asked Mar 06 '26 01:03

John Riselvato


1 Answers

In a SQL LIKE expression, the _ means any single character, and % means any characters.

  • The first line escapes the % and _ characters to remove their special meaning (adding a \ before them removes their special meaning)
  • The second line unescapes the % and _ characters that was preceeded by a NULL byte (char(0)).

The use of arrays in str_replace allows to do multiple replacements at once.

like image 105
Arnaud Le Blanc Avatar answered Mar 07 '26 16:03

Arnaud Le Blanc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!