I have a Japanese string like this (got from database)
$myString = "いっぱい< br />んどん"
Because the API requires the input string must be URL-encoded, so I encode it using
urlencode($myString);
The problem is the API platform page shows that string in a textarea.
As you know, the HTML texarea doesn't allow a line break using HTML < br />. It only allows "& #10;" to show a line break.
So now I don't know how to show $myString in the textarea of API platform.
My working flow: My website -> pass $myString to API -> $myString shows in API platform page (not mine)
Update:
The API is like this
< a href="diary:self?guid=ON&url=http%3A%2F%2Fredirect_uri.com&subject=%83e%83X%83g&body=....">Something< /a>
I have to pass myString to body=...
Update:
This is what I tried:
function convertBrTagtoASCII($string) {
$string = str_replace(array("</br>", "</br >", "<br>", "<br >"), " ", $string);
return $string;
}
$converted = convertBrTagtoASCII($myString);
Replace all br tag with & #10;, then urlencode($converted), then pass to "body" param of the API link. The output in textarea is
いっぱい んどん
It seems that the platform not only urldecode but also escapes the string. :(
Use htmlentities to escape special HTML characters, such as <
.
echo '<textarea>'.htmlentities($myString).'</textarea>'
In a textarea element you can use newlines, so there is no need to do special escaping on those.
Edit: Maybe I misunderstood the problem and you already have <br>
tags in your database. In that case, you can replace these tags with newlines. An alternative is to use a rich text edit box, such as TinyMCE, which does support HTML editing.
You do not have an encoding problem, you have an invalid data problem. The string you want to send to the API is not valid in the context of the API. You'd have the same problem with "Hello<br />World"
.
There are three possibilities:
"Hello<br />World"
or "いっぱい< br />んどん"
, in this case you could simply repair it (and its cousins) with"Hello World"
or "いっぱい んどん"
, in this case you would need a regex or similar tool to fix it up before urlencoding."Hello<br />World"
and "いっぱい< br />;んどん"
The main point ist, that this is not a property of the encoding or your code: Yes, you can try to fix obvious defects, but no there is no golden Rule
Convert your breaks to ascii. Then encode the URL. Then run a regex replace to convert the encoded line breaks
%26%2310%3B
back to