so I have a string that goes like this:
$string = "This is a test string. It has characters like these: '";
Is there a php function that transforms these to their correspondent character, in my example the desired output would be:
print $string
// OUTPUT: This is a test string. It has characters like these: '
yes there is:
htmlspecialchars_decode($string, ENT_QUOTES);
not sure about the specific ' char, as far as I know htmlspecialchars (with ENT_QUOTES flag) convert an apostrophe (') to ' (with a leading zero)
so the exact behavior on ' worth checking
EDIT:
I made the test and it does work :)
You can use html_entity_decode()
It's like reverse htmlentities. If you use quotes in your input string you have to set the second parameter of html_entity_decode() to ENT_QUOTES
See it in action: http://sandbox.onlinephpfunctions.com/code/7f4649eb47a8e639c514787a100b63bbad4bc8c6
Related
For some reason my server is ignoring "\t" -- yes I am using double quotes.
When I echo out a string like "abc\tdef\tghi" the "\t" is shown as a small "HT" with the "H" slightly smaller and above the "T"
Oddly, "\n" works and gives me a new line.
Not sure if this is an issue with PHP, NGINX, or something else, any ideas?
Try to use the the escape character instead the escape sequence. This works like a charm and you don't neet to worry about anything.
Let's say you have something like this:
$text = "First\tSecond\tThird\t...";
echo $text;
Replace it with this:
$text = "First\x09Second\x09Third\x09...";
echo $text;
Please note that using 'single quotes' with escape characters will get you '\x09' instead a real tabulator!
A list of all escape characters can be found in php.net manual:
PHP.NET escape characters
I have got few special characters in a string. I have removed most of them except couple of characters and those are
‘ and ’
This is not similar to ' '. I copied the ’ character from the browser.
Now my code looks like
$BadWords = array(",","'",":","+","&","...","(",")","?","%",".","!",'"');
$slug = str_replace($BadWords,"",$str);
echo $slug;
Even if I include those 2 chars in the array it doesn't remove from the string.Probably something needs to do with html decoding or something like that?
add these to your array like below and check
$special_quotes= array(chr(145),chr(146),chr(147),chr(148),chr(151));
$BadWords = array_merge($special_quotes,$BadWords);
I want to replace linebreaks with ' ' in PHP. Somehow I can't get it to work on this json encoded string [[0,"Hello World"],[1,"s\n"]] with $x = preg_replace('/\r\n|\r|\n\r|\n/m', ' ', $x);.
I'm out of ideas. And i know that the php code works with none-json encoded strings. Any ideas to fix this problem
Forgot this:
When I input the string as $xthe function or php code returns the same string. Instead of replacing \n with ' '.
I have also tried all relevant problems in Stackoverflow. none of them successful
preg_replace will try to parse the '\n' as an actual newline character, so you need some more escaping in there.
$x = preg_replace('/\\\r\\\n|\\\r|\\\n\\\r|\\\n/m', ' ', $x);
This is all kind of ugly though. Is there a reason you can't do a replace in the actual decoded strings instead?
I am retrieving a product description value stored in database from admin through textarea upon form submit. When I select the description from database I get $description = $row['description']; and I would like to echo $description on main page like this: echo nl2br($description); but I see "\r\n" characters instead of making new rows. From what I've found here and on the net, your string must be used between double quotes, like this:
echo nl2br("Hello, \r\n This is the description");
Now, the value of $description from database is in fact "Hello, \r\n This is the description" but in my script I have to use it like this:
echo nl2br($description);
Which does not make br's, it is outputing \r\n instead. So, what can I do, I can't use double quotes here, from my experience.
You could translate them into their respective escape sequences before passing the string through nl2br(), like this:
$description = nl2br(str_replace('\\r\\n', "\r\n", $description));
But what are the literal escapes doing in your database in the first place?
You are storing the literal value of \r\n in your database, not the actual characters they represent.
Verify this in your database. If you see \r\n in the description field, then you're probably escaping the backslash when you're storing the data.
It looks like your text contains the individual characters \, r, \, and n, and does not contain actual newline characters. As such, str_replace() should get the job done:
echo str_replace('\r\n', '<br>', $description);
The nl2br can take a second (optional) argument for "is_xhtml" which will convert the \r\n into a <br> for you. Just change your line to:
echo nl2br($description, TRUE);
I have a huge text that i keep getting in a json format. When i receive them in json, for some special characters like ' " ©, i receive them differently. i am using php and json to convert json into html. for example, i receive
' as \c101d (single quote)
" as \c201d (opening quote)
" as \c202d (closing quote)
I am planning to keep all the ', " into an array and use that array to replace the \c101d values in the text to ' or something like that so that it is easier to check the whole text in one command, replace all the special characters properly and display them correctly on my webpage.
Maybe some like $arr=array("\c101d"=>"'", "\c202d"=>""") and then call this array on the $text variable to check for characters similar to that in the array and do a string replace.
I have the idea but coding-wise how do i achieve this? Appreciate any help.
SOLVED
Well this piece of code solved all the problems, including ' , " , and all other weird characters.
$newtext=mb_convert_encoding($text, 'HTML-ENTITIES','UTF-8');
¿Are you using json_encode() with the different option flags?
For the substring replacement you should use strtr()
str_replace should do what you want.
This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.
The str_replace function takes arrays as possible parameters for search and replace, so you could do something like:
$search = array( '\'' , '"', ...);
$replace = array( '\c101d', '\c201d', ...);
$text = str_replace($search, $replace, $text);