Remove single quote from string using php - php

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);

Related

PHP stripping out \t to insert tab

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

How to fix this character in a string "’". Not working with json_encode

In mysql table, i have data having this character ’.
Like:
Francesca’s Baker
But when i use json_encode it gives null instead of the string. The problem i have found is with this character ’ or similar special characters.
Any ideas how to fix this?
Have you tried this?
<?php
$data=array("test"=>utf8_encode("Francesca’s Baker"));
echo json_encode($data);
Returns {"test":"Francesca\u0092s Baker"}
Single quotes may throw errors randomly, it gets really annoying!!!
You need to escape the single quote!
There are two solutions to your problem.
Solution 1:
Replace all ' with \'
str_replace("\'","\\\'",$string);
This will replace ' with \' from the string $string.
Solution 2:
$newstring = htmlentities($string);
This will change any special characters in the string $string to html entities, so when the string $newstring is printed to the screen it will look normal.If you need further help, let me know!
I had that problem to capture json and I solved with
$data = array(
'result' => utf8_encode($xxx),
);
echo json_encode($data);

Replace ' and similar html codes with their correspondent character?

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

replace line breaks in a json encoded string error?

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?

array of special characters to replace in a text, php, json

I have a huge text that i keep getting in a json format. When i receive them in json, for some special characters like ' " &copy, 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);

Categories