Pass string in this function.
function clean($string){
$string = str_replace(' ', '-', $string); // Replaces spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
For more info, check this Remove Special Character - Stackoverflow
Answer from Nana Partykar on Stack Overflowmysql - escape special character from string in php - Stack Overflow
PHP escape character in strings - PHP - SitePoint Forums | Web Development & Design Community
Escape string with PHP and HTML - Stack Overflow
How to interpret escape characters in the browser?
tags. It is mainly concerned with quotes, ampersands, and other special characters. https://www.php.net/manual/en/function.htmlspecialchars.php Note: There is no tag for a tab (\t) in HTML. In this case, you probably don't htmlspecialcharacters(). It is useful because it can prevent Javascript from being output to the page. Imagine some text like this: "". What is looks like you have is a tab separated CSV file. https://www.php.net/manual/en/function.fgetcsv.php Each line will be broken up into a list of items. You can use array_map() to apply htmlspecialcharacters() to each printable item in the list. Then you can combine all the list items into a string with implode(). $order = fgetcsv($fp, 1000, "\t"); $order = array_map('htmlspecialchars', $order); echo implode(" ", $order) . "
"; This is one way of doing this if you want to avoid Javascript injection. More on reddit.com
Why do we use escape characters in PHP?
What are the most common escape characters in PHP?
\n– new line\t– tab space\\– backslash\"– double quote\'– single quote (only in single-quoted strings)
How do I escape double quotes in PHP?
echo "She said, \"Hello\"";Pass string in this function.
function clean($string){
$string = str_replace(' ', '-', $string); // Replaces spaces with hyphens.
return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
For more info, check this Remove Special Character - Stackoverflow
The mysqli_real_escape_string() function escapes special characters in a string for use in an SQL statement.
Syntax:
mysqli_real_escape_string(connection,escapestring);
Example Escape special characters in a string:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$age = mysqli_real_escape_string($con, $_POST['age']);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('$firstname', '$lastname', '$age')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
connection Required. Specifies the MySQL connection to use
escapestring Required. The string to be escaped. Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
Example 1: Variable between single quotes
If you use single quotes everything between them will always be treated as part of the string.
$output .= '<div class="tab-pane" id="' . $type . '">";
Example 2: Variable between double quotes (option 1)
If you have a variable that you want to pass in a string you can just put it in there if you use double quotes and de variable is nog 'touching' the other words. It should always have spaces.
$output .= "<p>i would like to $your_text_here with you.</p>";
Example 3: Escaping quotes in a string
Escaping characters in a string can be done by using a \ (backslash) before the character you want to escape.
$output .= "<div class=\"tab-pane\" id=\"example-id\">";
Example 4: Variable between double quotes without spaces next to it
You can place your variable between {} braces if you use double quotes (option 2)
$output .= "<div class=\"tab-pane\" id=\"{$type}\">";
This question was however already answered in Mixing PHP variable with string literal
Your first block is doing string replacements, but then you use the ORIGINAL string, not the replaced one:
$output .= '<div class="tab-pane" id="' . $functionName . '">';
would be more correct. On the second one, you're escaping the ' quotes, which means that you never terminate the string, meaning that the . $type . portion is treated as plaintext within the string, not a PHP concatenation operation. Try
$output .= '<div class="tab-pane" id="' . $type . '">';
instead. note the LACK of backslash escapes.
And of course, you could use a HEREDOC, eliminating any need to escape quotes entirely:
$output .= <<<EOL
<div class="tab-pane" id="{$functioName}">
EOL;
I'm working through the PHP and MySQL Web Development (5th Edition) and am trying to display a plain text file in the browser but am unable to interpret escape characters such as \t and \n. They show up in the output although I'd like for them to be interpreted as tabs and newlines, respectively.
Code:
<?php
$document_root = $_SERVER['DOCUMENT_ROOT'];
?>
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Customer Orders</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Customer Orders</h2>
<?php
@$fp = fopen("$document_root/../orders/orders.txt", 'rb');
flock($fp, LOCK_SH);
if (!$fp) {
echo "<p><strong>No orders pending.<br />
Please try again later.</strong></p>";
exit;
}
while (!feof($fp)) {
$order = fgets($fp);
echo htmlspecialchars($order)."<br />";
}
flock($fp, LOCK_UN);
fclose($fp);
?>
</body>
</html>Expected output:
2 tires 2 oil 2 spark plugs Address 1 2 tires 4 oil 6 spark plugs Address 2
Actual output (same as the literal plain text file):
2 tires\t2 oil\t2 spark plugs\tAddress 1\n2 tires\t4 oil\t6 spark plugs\tAddress 2\n
I must be missing something seemingly trivial, although I'm following the book's code to the letter.
Any help would be greatly appreciated. Thank You!
tags. It is mainly concerned with quotes, ampersands, and other special characters. https://www.php.net/manual/en/function.htmlspecialchars.php Note: There is no tag for a tab (\t) in HTML. In this case, you probably don't htmlspecialcharacters(). It is useful because it can prevent Javascript from being output to the page. Imagine some text like this: "". What is looks like you have is a tab separated CSV file. https://www.php.net/manual/en/function.fgetcsv.php Each line will be broken up into a list of items. You can use array_map() to apply htmlspecialcharacters() to each printable item in the list. Then you can combine all the list items into a string with implode(). $order = fgetcsv($fp, 1000, "\t"); $order = array_map('htmlspecialchars', $order); echo implode(" ", $order) . "
"; This is one way of doing this if you want to avoid Javascript injection.
….
I have a website that lets the user build a dynamic form (think tabs with widgets) and then reduce the whole thing to a string with json.stringify().
But when I pass it to PHP (on one of our two systems to make things weirder) it adds a mess of escape characters. For example:
{\\"tab_type\\":\\"accordion\\",\\"options\\"{\\"tab::style\\":\\"color:0x8000ff00\\", When the correct formatting should look like:
"type":"accordion","options":{"tab::style":"color:0x8000ff00" The json is stringified properly on the JS side but it adds the slashes as soon as I look at the variable in the GET/POST on the PHP side.