First of all it is recommended to use the mysqli
along with prepared statement since it will avoid the SQL Injections that will occur. Now your code is purely injectable and it can be rectified with the help of mysqli
along with prepared statements or with the help of PDO
.
- Mysqli with Prepared Statement: http://php.net/manual/en/mysqli.prepare.php
- PDO: http://php.net/manual/en/book.pdo.php
- PDO with Prepared: http://php.net/manual/en/pdo.prepare.php
Explanations
As per the usage of trim()
in your variable you will be getting the strategy as per this alone.
trim
- Strip whitespace (or other characters) from the beginning and end of a string
Description: This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:
- " " (ASCII 32 (0x20)), an ordinary space.
- "\t" (ASCII 9 (0x09)), a tab.
- "\n" (ASCII 10 (0x0A)), a new line (line feed).
- "\r" (ASCII 13 (0x0D)), a carriage return.
- "\0" (ASCII 0 (0x00)), the NUL-byte.
- "\x0B" (ASCII 11 (0x0B)), a vertical tab.
Note:
But trim()
does not remove
the white space which is present at the middle of the string
that is given.
Example:
trim()
trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim('abc', 'bad') removes both
'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas trim('abc', 'b')
seemingly does not.
Scenario: Hence in order to remove all teh white space that is present in the string you have to use the following.
- You have to first remove all the character other that alpha numeric and white spaces with the help of
preg_replace()
function.
- After replacing all the above mentioned items you have to then trim upon the variable so that it will remove all the white spaces that has been present and hence your string will look as the string which you give in hard code or directly.
3. You can directly adopt the method by strong the trimmed value into a variable and then echo it.
preg_match
- Perform a regular expression match
Description: Searches subject for a match to the regular expression given in pattern.
Return Values: preg_match()
returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
Solution to your Problem
But when I use 1934 Heron Ave Unit D Schaumburg IL 60193 (that's my variable value) instead of $res['propertyaddress'] then it's working.
Reason: This Error occurs when you printing the values directly from the Database.
- If you have used any editor it will store the content directly to the DB as HTML tags alone.
- Hence in order remove the HTML tags you have first store the DB value into a variable by replacing all the values and then you have to display it.
- If you echo it directly you will not be seeing the HTML tags but if you view it by using
CTRL+U
you will be seeing it in the seeing it and it is not recommended. Hence you have to remove or strip of the parameters and then trim it.
Query:
preg_replace("/(\W)+/", "", $word_to_undergo);
Note: \W
- Anything that isn't a letter, number or underscore.
So, in terms of Unicode character classes, \W
is equivalent to every character that are not in the L
or N
character classes and that aren't the underscore character.
Alternative Solution:
To remove just put a plain space into your character class:
Query:
$needed_text = preg_replace("/[^A-Za-z0-9 ]/", "", $word_to_undergo);
Along with the above Solution you have to preform the trim so that it produces a perfect string as per your choice and it will match up with the query and produce the result.
As per Suggestion One: It should be
$final_value = preg_replace("/(\W)+/", "", $word_to_undergo);
$final_value = preg_replace("/(\W)+/", "", $res['propertyaddress']);
As per Suggestion Two: It should be
$final_value = preg_replace("/[^A-Za-z0-9 ]/", "", $word_to_undergo);
$final_value = preg_replace("/[^A-Za-z0-9 ]/", "", $res['propertyaddress']);
Addition to the above solution you can try using like this to.
<?php
$display=trim($res['propertyaddress']);
echo $display;
?>
pdetail=".$pdetail=trim($res['propertyaddress']); };` – M. I. Sep 23 '16 at 14:39