PHP Replacing \" and \' with " and ' - php

I am trying to go through the array string I called out from database and filtered to a readable state. The string could have a lot of \' and \", below is just an example.
$content = 'It\'s go to somewhere \"GREAT\"!';
I am trying to use str_replace but it is not working...
$content1= str_replace('\\\'', "'", $content );
$newcontent= str_replace('\\\"', '"', $content1 );
Output should be
It's go to somewhere "GREAT"!
instead.. I get
It\'s go to somewhere \"GREAT\"!
I looked at preg_replace, but I don't quite get all the /.. or where to start on it.
Please help.

Here's how
$content = 'It\'s go to somewhere \"GREAT\"!';
$content = stripslashes($content);
echo $content;

What you want to use is stripslashes($str).
Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\) are made into a single backslash ().
$str = "Is your name O\'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);

Related

Echo text including double quotes in PHP

I have a $_GET[q] from the URL. I am trying to ECHO the search term back into the search box. Sometimes people might submit queries encapsulated in quotes and in these cases the ECHO interprets the search term
ECHO $_GET[q];
as:
ECHO ""search term"";
and as a result I get a blank search box. Search queries with a single quote, like: Peter's house, work fine.
When I use:
mysqli_real_escape_string($conn, $_GET[q])
I only get a backslash in the search box.
How could I populate the search box with a search term encapsulated in double quotes?
You could also use:
echo str_replace('"','',$_GET[q]);
This will of course remove all double quotes, so if they may be valid somewhere in the search term then better might be:
echo str_replace('"','"',$_GET[q]);
Haven't tested this but this might also work:
echo html_entity_decode(htmlentities($_GET[q))
You could use addslashes
like this:
$t = 'peter "pan"';
echo addslashes($t); // outputs: peter \"pan\"
You can try:
$str = "Hello World!";
echo $str . "<br>";
echo chop($str,"");
Ouptput:
Hello World
Explanation:
The chop() function, helps you chop off the quotes anyone might add to string.
You can manipulate as appropriate for your code.
$str = preg_replace( '["|\']','', $_REQUEST['q'] );
echo( $str ); //no double, no single quotes, faster than str_replace when you have to make more than 1 call to str_replace
or...
$str = str_replace( '"','', $_REQUEST['q'] ); //no double quotes, faster than preg_replace when you only make one call to str_replace
echo( $str ); //no double quotes

How to delete quotation marks from text array?

My code simply displays a random line from a text file,
But in my text file most of the proxies look like this: "11.15.19.15:80" I need help how to display only the address on the site, and remove the quotation marks.
<?php
$message_array = file("proxies.txt");
$message = array_rand($message_array);
echo "$message_array[$message]";
?>
All you need to do is wrap the string in a trim():
<?php
$message_array = file("proxies.txt");
$message = array_rand($message_array);
echo trim($message_array[$message], "\""); // 11.15.19.15:80
?>
Note that the second argument in trim() is needed, because your string contains the " characters, rather than uses them to denote the string itself. Adding an escaped backslash ("\"") removes the quotation marks from what is inside the string itself.
I've created a simple demonstration of this at 3v4l.org here.
I think you can use a regex like so:
preg_replace('/["]*/g', '', $message);
Use str_replace. Here is an example:
$message = str_replace('"', '',$message);
You can use the trim function if you just need to remove the double quotes:
$message = trim($message, '"');
Also str_replace:
$message = str_replace('"', '', $message);

Escaping single quotes in a URL link

I have a link that is sent throw some PHP code:
echo "<a href='" . $galerry . "#" . apastro(get_title($primid)) . "' class='linkorange'>voir sa galerie</a>";
$galerry links to another page.
get_title($primid) is the id of a specific element in $galerry page.
And the mechanism works fine until one of the elements id has a single quote in it. Which makes sense as it would interrupt the echo function.
This is why I have the apastro function:
function apastro($phrase){
$phrase1 = str_replace("'", "\'", $phrase);
return $phrase1;
}
Yet, the \ before the single quote isn't helping...
So let's say the link redirects to the element with id="l'aro" on the page something.php. Then the URL will be something.php#l\.
it would interrupt the echo function
It wouldn't. It would break a string literal delimited by ' characters, but your string literal is delimited with " characters. In this case, it is breaking the HTML attribute value which is delimited by ' characters.
\ is not an escape character for URLs or HTML.
Use urlencode to make a string safe to put into a URL.
Use htmlspecialchars to make a string safe to put into an HTML attribute.
$title = get_title($primid);
$urlsafe_title = urlencode($title);
$url = $galerry . "#" . $urlsafe_title;
$htmlsafe_url = htmlspecialchars($url, ENT_QUOTES | ENT_HTML5);
echo "<a href='$htmlsafe_url' class='linkorange'>voir sa galerie</a>";
If you're looking to escape single quotes only, use double backslashes, as follows
$str = str_replace("'", "\\'", $str);

how do i remove the this "\r\n" in my string

I have problem in sending message to my client via socket,the string that I would like to send is like this "##w32,12345678,xxx,5*zy\r\n"
$msg = $_POST['comm_input']; //"##w32,12345678,xxx,5*zy\r\n"
if this is posted i get the value of $msg which is "##w32,12345678,xxx,5*zy\r\n"
but my client will not accept this kind of message..but if I manually do like this without posting the comm_input;
$testmsg = "##w32,12345678,xxx,5*zy\r\n";
It works fine,I tried to look at in firebug there is no double quotes and \r\n.and it works fine.
if I post the comm_input.and look at in the firebug there is double quotes and \r\n,how can I remove this.
You can use str_replace function to remove \r\n.
DEMO
<?php
$testmsg = "##w32,12345678,xxx,5*zy\r\n"; <-- $_POST value
$order = "\r\n";
$replace = "";
$newstr = str_replace($order, $replace, $testmsg);
echo $newstr; //outputs ##w32,12345678,xxx,5*zy
?>
using str_replace, you need to escape the \ with and extra \, hence, \r as string becomes \\r
$msg = $_POST['comm_input']; //"##w32,12345678,xxx,5*zy\r\n" ;
$new_msg = str_replace("\\r\\n", "", $msg);
Edit: to remove double quotes
$new_msg = str_replace('"', "", $new_msg);
Consider reading this article : Escape Sequence in PHP
you can use
$msg = "##w32,12345678,xxx,5*zy\r\n";
$str = rtrim($msg);
Refer trim() and rtrim()

PHP new line problem

simple problem baffling me...
i have a function:
function spitHTML() {
$html = '
<div>This is my title</div>\n
<div>This is a second div</div>';
return $html
}
echo $spitHTML();
Why is this actually spitting out the \n's?
Backslashes used in single quote strings do not work as escape characters (besides for the single quote itself).
$string1 = "\n"; // this is a newline
$string2 = '\n'; // this is a backslash followed by the letter n
$string3 = '\''; // this is a single quote
$string3 = "\""; // this is a double quote
So why use single quotes at all? The answer is simple: If you want to print, for example, HTML code, in which naturally there are a lot of double quotes, wrapping the string in single quotes is much more readable:
$html = '<div class="heading" style="align: center" id="content">';
This is far better than
$html = "<div class=\"heading\" style=\"align: center\" id=\"content\">";
Besides that, since PHP doesn't have to parse the single quote strings for variables and/or escaped characters, it processes these strings a bit faster.
Personally, I always use single quotes and attach newline characters from double quotes. This then looks like
$text = 'This is a standard text with non-processed $vars followed by a newline' . "\n";
But that's just a matter of taste :o)
Because you're using single quotes - change to double quotes and it will behave as you expect.
See the documentation for Single quoted strings.
Change ' to " :) (After that, all special chars and variable be noticed)
$html = "
<div>This is my title</div>\n
<div>This is a second div</div>";

Categories