Convert TYPO3 locallang values into json file - php

I have created plugin and I have used locallang for labels.
I am working on TYPO3 10 and I have used many locallang values in my template file for labels.
\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('your_label_key','your_extensionName');
I tried this code it works fine, but it is only for one key or label.
I want all the translation in json formant (json file), is there any way to do that ?
Thanks in advance.

I found an answer in another way.
It look like below,
$fileContents= file_get_contents($url);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
First i store my locallang file into variable with file_get_contents($url);
Then with json_encode($xml),
All locallang values converted into json format.

Related

Wrapping String PHP

I have a problem with my code, i have this code that create image from external source of image & string. I used json to get the string.
My problem is if i used the string from json data i could not get the proper wrapping of string like this:
http://prntscr.com/dbhg4n
$url = 'https://bible-api.com/Psalm100:4-5?translation=kjv';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$string = $data->text;
But if i declare and set string directly i got the output that i want like this:
http://prntscr.com/dbhg7q
$string = "Enter into his gates with thanksgiving, and into his courts with praise: be thankful unto him, and bless his name. For the Lord is good; his mercy is everlasting; and his truth endureth to all generations.";
I dont think the error or the problem is on the code for wrapping the text on my image. I think it is on the json data. How can i fix this?
The text has \n symblols. Just replace them:
$string = preg_replace("/\n/", ' ', $data->text);
or without a regular expression:
$string = str_replace("\n", ' ', $data->text);

file_get_contents() skipping text between <> tag

I am trying to read a .tsv file using PHP. I am using the simplest method of file_get_contents() but it is skipping any text between <> tags.
Following is the format of my .tsv file
<id_svyx35_88c_avbfa5> <Kuldeep_Raval> rdf:type <wikicat_Delhi_Daredevils_cricketers>
Following is the code I am using
$filename = "access_s.tsv";
$content = file_get_contents($filename);
//Split file into lines
$lines = explode("\n", $content);
echo $content;
On reading it, the output is just
rdf:type
Please help in what can be the solution to read the line as it is?
Try to apply htmlspecialchars() to $content:
$filename = "access_s.tsv";
$content = htmlspecialchars(file_get_contents($filename));
//Split file into lines
$lines = explode("\n", $content);
echo $content;
Reference on php.net
The tags have always been there, the browser just does not show them. Just like with any valid HTML tag, you can see them when viewing the source code of the website.

remove fake comments from json file

since json dosn't support comments I need my own function to clean my comments
My comments are css style, like this
/*comment*/
i tryed the following
$json = preg_replace("/(\/\*.?\*\/)/", "", $json);
but no luck.
thank's
echo preg_replace("#/\*.*?\*/#s", "", $json);
Notable changes:
I used # as the pattern delimiter. By doing this, I don't need to
escape forward slashes, making the regex prettier to read.
I added the s flag, which makes the . also match new line characters.
Beware, this will destroy comments inside a json string. An example json object that will get clobbered
{"codeSample": " /*******THIS WILL GET STRIPPED OUT******/"}
Use the following:
$json = preg_replace('!/\*.*?\*/!s', '', $json); // remove comments
$json = preg_replace('/\n\s*\n/', "\n", $json); // remove empty lines that can create errors
This will erase comments, multi line comments and empty lines
EDIT: as some of the guys were saying in the comments, you can use:
$json = preg_replace('/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/', '', $json);
To remove only comments that are not found within strings.
$string = "some text /*comment goes here*/ some text again /*some comment again*/";
$string = preg_replace( '/\s*(?!<\")\/\*[^\*]+\*\/(?!\")\s*/' , '' , $string );
echo $string; // some textsome text again
The complete php code to remove both single and multi-line comments.
$json = preg_replace('!/\*.*?\*/!s', '', $json); //Strip multi-line comments: '/* comment */'
$json = preg_replace('!//.*!', '', $json); //Strip single-line comments: '// comment'
$json = preg_replace('/\n\s*\n/', "\n", $json); //Remove empty-lines (as clean up for above)
Here a site you can test the code: https://www.phpliveregex.com
To test the first code line fill in like this picture:

Replacing \r\n (newline characters) after running json_encode

So when I run json_encode, it grabs the \r\n from MySQL aswell. I have tried rewriting strings in the database to no avail. I have tried changing the encoding in MySQL from the default latin1_swedish_ci to ascii_bin and utf8_bin. I have done tons of str_replace and chr(10), chr(13) stuff. I don't know what else to say or do so I'm gonna just leave this here....
$json = json_encode($new);
if(isset($_GET['pretty'])) {
echo str_replace("\/", "/", jsonReadable(parse($json)));
} else {
$json = str_replace("\/", "/", $json);
echo parse($json);
}
The jsonReadable function is from here and the parse function is from here. The str_replaces that are already in there are because I am getting weird formatted html tags like </h1>. Finally, $new is an array which is crafted above. Full code upon request.
Help me StackOverflow. You're my only hope
Does the string contain "\r\n" (as in 0x0D 0x0A) or the literal string '\r\n'? If it's the former, this should remove any newlines.
$json = preg_replace("!\r?\n!", "", $json);
Optionally, replace the second parameter "" with "<br />" if you'd like to replace the newlines with a br tag. For the latter case, try the following:
$json = preg_replace('!\\r?\\n!', "", $json);
Don't replace it in the JSON, replace it in the source before you encode it.
I had a similar issue, i used:
$p_num = trim($this->recp);
$p_num = str_replace("\n", "", $p_num);
$p_num = str_replace("\r", ",", $p_num);
$p_num = str_replace("\n",',', $p_num);
$p_num = rtrim($p_num, "\x00..\x1F");
Not sure if this will help with your requirements.

XML not parsing correctly for no clear reason

I have my XML feed being created from an associative array.
Using new DOMDocument('1.0','UTF-8');
and it was working fine until I made some changes to the methods used to convert it from iso-8895-1 to UTF-8.
The character being flagged up is the letter R, but I have gone into the file via ftp and changed it manually (deleted and retyped r in case of any errors).
I changed it from $summ = str_replace(chr(10),"",$summ); to $summ = str_replace(array("\n", "\r", "\r\n"),"",$summ); but as you can see below I have changed it back and nothing. I have also set DOMDocument to formatoutput=false, because it was previously set on true, but it all that did was move the problem to a different letter r, both are on the end of words?????
//$summ = str_replace(array("\n", "\r", "\r\n"),"",$summ);
$summ = str_replace(chr(10),"",$summ);
/*$v = str_replace(" "," ",$v);
$v = str_replace(" ","",$v);*/
$summ = iconv("ISO-8859-1", "UTF-8//TRANSLIT",$summ);
$summ = str_replace("£","£",$summ);
//$summ = htmlentities($summ, ENT_QUOTES, "UTF-8");
$this->summary = addslashes($summ);
Sorry for the unclear question but I couldn't show code for reasons of the output beind sensitive.
Anyway, the problem was htmlentities, and htmlspecialchars caused the same problem, without them it works fine.

Categories