"\/" text shows up within my JSON link values [duplicate] - php

This question already has answers here:
json_encode() escaping forward slashes
(4 answers)
Closed 4 years ago.
My database contains links to images which are displayed properly within their structure. When I run my PHP code, the outputted JSON values are the same image links which fail to load because the links keep being outputted like this:
https:\/\/i.ebayimg.com\/00\/s\/NDQwWDgwMA==\/z\/ViAAAOSwhmtbN7fe\/$_59.JPG\r\n
Even though the database displays it like this:
https://i.ebayimg.com/00/s/NDQwWDgwMA==/z/ViAAAOSwhmtbN7fe/$_59.JPG
Is there something wrong with my PHP code?

you can use string replace function in php for remove (\)
$your_string = str_replace("\\", "", $your_string);

Simple use stripslashes()
First remove \r\n using str_replace() from link/url and then apply stripslashes()
$link = 'https:\/\/i.ebayimg.com\/00\/s\/NDQwWDgwMA==\/z\/ViAAAOSwhmtbN7fe\/$_59.JPG\r\n';
$link = stripslashes( str_replace("\\r\\n", '', $link) );
echo $link;

Related

Can't replace variable names in string [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
I want to send a system message that addresses the user with his first name. The message is stored in a .txt file as:
Hello $user->firstname
Login link: something.something/user/id
In the userController (where the message is sent from) I'm now trying to replace the $user->firstname with the actual $user->firstname:
$output = file_get_contents(Yii::$app->basePath."message.txt");
$user = $this->findModel($id); //this is tested and works
$output = str_replace("$user->firstname", $user->firstname, $output);
However, my output after this is still the exact same as in the text file. What am I doing wrong?
I think it might be as simple as using single quotes in your str_replace call:
$output = str_replace('$user->firstname', $user->firstname, $output);
When you use double quotes, PHP has already tried to replace the string before calling str_replace.
See https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing for more information.
$output = str_replace("$user->firstname", $user->firstname, $output);
Variables inside double quotes get replaced - so you are not trying to replace the text $user->firstname here, you are trying to replace the text George (assuming that was the users first name) - but there is no George in your input text, so … nothing to replace.
Use single quotes, or escape the $ sign with a \

How can I remove a portion of a string from the right side of a string starting at a special character [duplicate]

This question already has answers here:
Break up/parse a URL into its constituent parts in php
(1 answer)
How to remove the querystring and get only the URL?
(16 answers)
Closed 3 years ago.
I have a series of strings created as URLs as the page gets refreshed.I want to remove a portion from the right side of these strings. The strings look like:
http://funfun.ca/?image=1
http://funfun.ca/?image=14
http://funfun.ca/?image=217
and so on. The goal is for anything starting with "?" to be removed from the right side of the strings and leave the following for all of them:
http://funfun.ca/
I appreciate any help I can have to solve this
You may explode the url string for ? and save only the first part obtained:
$firstPart = explode('?', $url)[0];
Example:
echo $firstPart = explode('?', 'http://funfun.ca/?image=1')[0];
returns:
http://funfun.ca/

How can I ignore an apostrophe within a quote in php? [duplicate]

This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
I have a php variable referring to a string that contains apostrophes, but when I quote this variable, it thinks I am trying to end the string. My variable is reading from an array of table data, so I can not go in and put a "\" before every apostrophe in the table. If $foo contains the string "don't", how do I correctly say '$foo' without it trying to end the string. Thanks.
You are correct in thinking that you need to add escape characters ("\") before the apostrophes.
To do this on the fly with the database data you can use the php function addslashes.
so:
$escapedString = addslashes($string);
You could also do this with the string replace function for higher precision:
$escapedString = str_replace("'", "\'", $string);
You can use PHP's addslashes PHP Manual - Add slashes
$foo = addslashes($foo);

Remove div php - part of string [duplicate]

This question already has answers here:
How to remove text between tags in php?
(6 answers)
Strip HTML tags and its contents
(2 answers)
Closed 9 years ago.
I want to remove part of string between two html tags. I have something like this:
$variable = "This is something that I don't want to delete<blockquote>This is I want to delete </blockquote>";
the problem is that the string between blockquote tag is changing, and its need to be deleted, no matter what it is. Anyone now how?
Regex are not the best thing to parse html string, you should take a look at Simple HTML dom parser or the php DOMDocument class.
If you still want to use a regex in this case it will be for example :
$variable = preg_replace('/<blockquote>.+<\/blockquote>/siU', '', $variable);
Test it there.
You can use regular expressions, but this is by no means fail-safe and should only be used in trivial cases. A better way is to use a full-fledged HTML parser.
<?php
$str = preg_replace('#<blockquote>.*</blockquote>#siU', '', $str);
?>
please try using regex in this way
<?php
$variable = "This is something that I don't want to delete<blockquote>This is I want to delete </blockquote>";
$str = preg_replace('#(<blockquote>).*?(</blockquote>)#', '$1$2', $variable);
print($str);
?>

How to have apostrophes in PHP string? [duplicate]

This question already has answers here:
php parameter with apostrophes
(2 answers)
Closed 2 years ago.
Part of my PHP code includes the construction of a URL string as follows:
$msg = $_REQUEST["msg"];
$content = 'action=sendmsg'.
'&user='.rawurlencode($username).
'&password='.rawurlencode($password).
'&to='.rawurlencode($destination).
'&text='.rawurlencode($msg);
When $msg happens to contain an apostrophe, it get sent as "\'".
What do I need to do to make sure the backslash is not inserted by PHP?
You probably want to check out stripslashes: http://php.net/manual/en/function.stripslashes.php
Assume you want to send the $content variable instead of just stripping the backslashes, Consider to use urlencode() instead of rawurlencode(). Also, you can use the function once for your $content variable.
$content = urlencode($content);
UPDATE: both urlencode and rawurlencode may not fit your case. Why don't you just send out the $content without URL encode? How do you send our the query string? If you are using cURL, you do not need to encode the parameters.
You can try
Stipslashes
or put the string in "" and add ' where you want.

Categories