Getting rid of escape characters in PHP - php

I've got a string containing values such as "hello world\' hello world\'" and I'd like to get rid of the escape characters (the backslashes.)
I've tried the following code:
str_replace("\\", "", $data);
But it doesn't seem to work.

If all you want to do is to get rid of backslashes, then there's a very handy PHP function that accomplishes just that
$var = stripslashes($var);

Assuming you're using $var as the last parameter in str_replace() instead of $data, it should work fine.
$var = "hello world\' hello world\'";
echo $var . "<br />";
echo str_replace("\\", "", $var) . "<br />";
Output:
hello world\' hello world\'
hello world' hello world'

this should work great for you you were not referencing the variable $var correctly in php replace subject parameter also assuming you need to replace the \' you were putting \ which searches for it hence nothing was found to be replaced hope this helps
$var = "hello world\' hello world\'";
echo str_replace("\'","",$var);

Related

PHP - Trim is not working when word comes through GET request

[PHP] - Trim is not working when word comes through GET request
My Script as Below:
if(isset($_GET['use'])){
$usage = $_GET['use'];
$str = "Hello World";
echo $str . "<br>";
echo rtrim($str,$usage);
}
and result should be:
when url end with = index.php?use=World
echo $str = Hello World
echo rtrim($str,$usage) == Hello
but this not work for me, why ?
When I run your example code it outputs
Hello
It seems you've done something wrong.
When I tried with
index.php?use=Worlds
It still outputs
Hello
When I tried
index.php?use=Worl
It outputs
Hello World

echo php variable and html in Wordpress

I am playing around with wordpress and wondering, why this line of code works:
echo "<a href='....'>$name</a>";
I learned it this way:
echo "<a href='....'>".$name."</a>";
Is there something special defined in WP to make this work?
In PHP, there are two types of String.
The first type uses the single quotes, as follows.
$val = 'this is a simple string';
The second type is as follows:
$val = "This is a not so simple string";
With the latter type, any variables included in the string will be resolved to their values, so:
$val = "Hello there";
$message = 'Dave says $val';
// Literally equals: Dave says $val
$message2 = "Dave says $val";
// Literally equals: Dave says Hello there
There are lots of other differences, which you can read about here.

How to escape backslashes in files

I am trying to work on a script but I am stuck in one place.
Eg. To get a php output I have used..
str_php = """
<?php
echo "Hello World!";
?>"""
php_file = open("index.php", "w")
php_file.write(str_php)
php_file.close()
Ok, so I get the output as it is....
<?php
echo "Hello World!";
?>
So my php code is running. all good till here. But, the problem starts from when I try using "\" and "\n" and "\r"
str_php = """
<?php
echo "Hello World!"; \n echo "How are you"; \n echo "God bless you";
?>"""
php_file = open("index.php", "w")
php_file.write(str_php)
php_file.close()
But here I dont get the output as it is.
<?php
echo "Hello World!";
echo "How are you";
echo "God bless you";
?>
And the "\" it just vanishes... at an output.
Eg. I want an output of a php hyperlink something like...
str_php = """<?php
print("$dirArray[$index]");
?>"""
php_file = open("index.php", "w")
php_file.write(str_php)
php_file.close()
and the output I get is...
<?php
print("$dirArray[$index]");
?>
The "\" is missing and the php does not run creating error.
print("$dirArray[$index]") - Original
print("$dirArray[$index]") - python output
Can any one help me out with "\", "\n", "\r" ??
Just use "\" to escape the "\" character.
Since it is common to want to have long strings containing several "\", Python also allows one
to prefix the string opening quotes if ar r (for "raw") - inside such
a string, no escaping of "\n" to chr(10) or "\t" to chr(9) happens:
>>> print (r"Hello \n world!")
Hello \n world!
You need to escape your "\" with another backslash writting it as "\\".
If you use "\n" it will be parsed and make a newline. Try to use '\n', strings enclosed in '\n' are not parsed and it should print out as you want it to.

PHP preg_repalce Replace %var% with text

I am trying to replace multiple variables in a text.
I have For example this text:
This is an example text , my variables are:
%name%
%frontname%
%lastname%
%email%
No I want to replace each variable that is between the % % characters with {$variable}.
So my output looks like this:
This is an example text , my variables are:
{$name}
{$frontname}
{$lastname}
{$email}
The pattern I am trying to use is this :
$textResponder = preg_replace('#\%[^\%]+\%#', '{$$1}', $text);
But that is not working, as I get this as output: {$} {$} {$}. Does somebody know what the correct pattern would be?
Thanks in advance
$var = "Hello, %test% World %another test%!";
echo preg_replace('#%(.*?)%#', '{\$$1}', $var);
Test here
Try
preg_replace('/%(.*?)%/', '{\$$1}', $text);
#Gert Van de Ven You need to escape the dollar's special meaning:
$var = "Hello, %test% World!";
echo preg_replace('#\%(.*?)\%#', '{\$$1}', $var);

PHP string inception manipulation

I cant get this to work and its driving me mad, pls help
echo "<a href='#' onclick='javascript:$.jGrowl
(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
the problem comes down to the 'product_description' - those single ' marks are breaking it, what should i do sigh
EDIT: if i replace the .$_SESSION['product_description'][$i]. with a bunch of charecters it works, its not a problem with anything but PHP and those ''
Chances are the real problem lies with the $.jGrowl. Within double quotes, PHP tries to parse found variables. e.g.
$foo = 'foo';
echo "This is foo: $foo"; // output: This is foo: foo
So, to avoid this you need to escape the $ using \$ within the string...
echo "...\$.jGrowl..."
See this demo.
Keep this here for reference:
Escape them with a backslash, like you would with double quotes. e.g.
// which ever quote is used to encapsulate the string
// must be escaped within the output.
echo 'Hello, \'world!\''; // output: Hello, 'world!'
echo "Hello, \"world!\""; // output: Hello, "world!"
// but, if you use the opposite quote, it does not need
// to be escaped for output.
echo 'Hello, "world!"'; // output: Hello, "world!"
echo "Hello, 'world!'"; // output: Hello, 'world!'
See the PHP Docs on strings for more information and what characters need escaping.
Are you missing your opening "?
echo "<a href='#' onclick='javascript:$.jGrowl(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
Make it
echo "<a href='#' onclick='javascript:$.jGrowl(\"".$_SESSION['product_description'][$i]."\")' >?</a>";
Try:
echo "<a href='#' onclick='javascript:\$.jGrowl(\"".str_replace('"','\"',$_SESSION['product_description'][$i])."\");' >?</a>";
You need to ensure you are escaping any double quotes within the value...
This one works for me:
echo "<a href=\"#\" onclick=\"javascript:$.jGrowl('".$_SESSION['product_description'][$i]."')\" >?</a>";

Categories