slash before every quote problem
Looks like you have magic quotes turned on. Use below condition using stripslashes
with whatever text you want to process:
if(get_magic_quotes_gpc())
{
$your_text = stripslashes($your_text);
}
Now you can process $your_text
variable normally.
Update:
Magic quotes are exaplained here. For well written code there is normally no harm in disabling it.
Add backslash before single and double quote
If perl
is okay:
perl -pe 's/"{3}(*SKIP)(*F)|[\x27"]/\\$&/g'
"{3}(*SKIP)(*F)
don't change triple double quotes- use
(\x27{3}|"{3})(*SKIP)(*F)
if you shouldn't change triple single/double quotes
- use
|[\x27"]
match single or double quotes\\$&
prefix\
to the matched portion
With sed, you can replace the triple quotes with newline character (since newline character cannot be present in pattern space for default line-by-line usage), then replace the single/double quote characters and then change newline characters back to triple quotes.
# assuming only triple double quotes are present
sed 's/"""/\n/g; s/[\x27"]/\\&/g; s/\n/"""/g'
Single Quotes inside Single Quotes removing Forward Slashes
It's not really removing the slashes. You'll see them if you view the page source rather than using inspect.
Put the style attribute value in double quotes.
$output .= "style=\"background:url('https://www.example.com/images/" . $profile_image_url . "')no-repeat;\"";
Or put the URL in double quotes.
$output .= "style='background:url(\"https://www.example.com/images/" . $profile_image_url . "\")no-repeat;'";
Unescaped single quotes inside single quotes is not possible. The second single quote encountered will close the first one. And escaping them doesn't work in that context, as you've seen.
String Replace - Add Slash Before Single Quote
Why not use addslashes?
$string = "I love Bob's Pizza!";
$string = addslashes($string);
echo $string;
UPDATE: If you insist on your way it's because you're not escaping the single quote. Try:
$string = 'I love Bob\'s Pizza!';
$string = str_replace("'", "\\'", $string);
echo $string;
You simply can't do what you're doing because it causes a syntax error.
how to add slashes to only double quotes
Delete your string $str =" or $str = '
as this is entirely wrong.
1) Use a CSS Style Sheet to store your CSS outside your HTML and avoid repetition.
2) Use single quotes in HTML syntax (and your PHP string is in double quotes) to avoid the need to escape your HTML at all (javascript elements will be an exception, however).
Example of points one and two combined:
CSS File
.body_wrap_table {
width: 100%;
}
.table_td_tr {
font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
margin: 0;
padding: 0;
}
HTML PHP String
$str = " <table class='body-wrap body_wrap_table'>
<tr class='table_td_tr'>
<td class='table_td_tr'>this is my test</td></tr></table>
";
So no need for quote escaping at all.
3) If you really want to do it you can use a Regex function such as Preg_replace
as exampled here:
$str = preg_replace('/"+(?<!\\")/', '\"', $str);
This regex will match any "
character which is not preceeded by a \
character, and the replace with an escaped quote.
4) A possibly simpler approach which may or may not be a solution for you (and less processor heavy than a Regex) is to simply use a str_replace such as :
$str = str_replace("\"","\\\",$str);
This replaces any text fitting the shape "
with \"
but this may cause double escaping if a quote already has an escape mark preceeding it.
References:
regex for matching something if it is not preceded by something else
use preg_replace to replace character unless an escape character precedes
Related Topics
Redirecting to Previous Page After Login - PHP
Why Is Pdo Better For Escaping MySQL Queries/Querystrings Than MySQL_Real_Escape_String
What Is the Function _Construct Used For
Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error
Doctrine 2 and Many-To-Many Link Table With an Extra Field
How to Get Enum Possible Values in a MySQL Database
Laravel Update Model With Unique Validation Rule For Attribute
How to Store File Name in Database, With Other Info While Uploading Image to Server Using PHP
How to Best Store User Information and User Login and Password
PHP - Redirect and Send Data Via Post
How to Know Which 'PHP.Ini' File Is Used
Restructure Multidimensional Array of Column Data into Multidimensional Array of Row Data
Switch PHP Versions on Commandline Ubuntu 16.04
Using Simplexml to Create an Xml Object from Scratch
Use One Bind_Param() With Variable Number of Input Vars