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);
Related
I need to convert all big integers to strings in my response json. Currently my regex look like this
preg_replace(
'/:\s*(-?\d{16,})/',
': "$1"',
$content
);
But problem with such regex is that if my response contains another json string then bigints inside it will be wrapped in string too, but without escaping. Is there any way to escape appended quotes in such case? Or maybe fix incorrect json with another regex?
Example
{"example_bigint": 3330922503411457761}
will be converted to
{"example_bigint": "3330922503411457761"}
but
{"example_json" : "{\"example_bigint\": 3330922503411457761}"}
will be converted to
{"example_json" : "{\"example_bigint\": "3330922503411457761"}"}
when expected output is
{"example_json" : "{\"example_bigint\": \"3330922503411457761\"}"}
There is a flag in the json_decode function:
$myJson = json_decode($jsonString, flags: JSON_BIGINT_AS_STRING);
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);
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
I'm working on transferring data from one database to another. For this I have to map some values (string) to integers and this is where I run into a strange problem.
The string looks like this $string = "word anotherword"; so two words (or one space).
When I explode the string or count the amount of spaces it misses the white space. Why? I var_dumped the variable and it says it's a string.
Below is the code i'm using.
echo "<strong>Phases</strong>: ".$fases = mapPhase($lijst[DB_PREFIX.'projectPhase']);
The string that's being send to the function is for example "Design Concept". This calls the following function (where the spaces get ignored)
function mapPhase($phases){
echo "Whitespace amount: ".substr_count($phases, ' ')."<br />";
}
For the example string given this function echoes 0. What's causing this and how can i fix it? The strangest thing is that for one instance the function worked perfectly.
More than one whitespaces (in HTML) are always converter into one whitespace. For example code indents.
If you want to print more than one, one by one use &nbps; instead.
function mapPhase($phases){
echo 'Whitespace amount: '.substr_count($phases, ' ').'<br />';
}
It may well be that the alleged space in the string may not be a space as in ' ', but something similar, which gets rendered in the browser in the same way as ' ' would. (for a rudimentary list of possible characters: http://php.net/manual/en/function.trim.php)
Thus, checking what the whitespace exactly is may be the solution to that problem.
Maybe they are not even spaces. Try ord() for each symbol in your string.
ord(' ') is 32.
You can use:
$string = preg_replace('/\s+/', '', $string);
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?