I have a reference emojis file used by my php code. Inside there is for example "woman-woman-boy", but the browser (chrome) replaces this name by "family_mothers_one_boy"...
Why are there two versions of emojis' names?
Is there en (some) error(s) in my file, or should I have to do something in my code to avoid the conversion?
NOTE:
The code related to this emoji is:
1F469;👩👦
Here are the two functions I'm using to manage the emojis:
1. When I display the emoji, I replace the tage :name: by the HTML rendering (using unicode)
function replaceEmojiNameByUnicode($inputText){
$emoji_unicode = getTabEmojiUnicode();
preg_match_all("/:([a-zA-Z0-9'_+-]+):/", $inputText, $emojis);
foreach ($emojis[1] as $emojiname) {
if (isset($emoji_unicode[$emojiname])) {
$inputText = str_replace(":".$emojiname.":", "&#x".$emoji_unicode[$emojiname].";", $inputText);
}
else {
$inputText = str_replace(":".$emojiname.":", "(:".$emojiname.":)", $inputText);
}
}
return $inputText;
}
2. When I want to propose the list of emoji I display an HTML SELECT in the page. Teh following function return the list of option to add inside:
/* Display the options in the HTML select */
function displayEmojisOptions(){
$emoji_unicode = getTabEmojiUnicode();
foreach ($emoji_unicode as $name => $unicode) {
echo '<option value="&#x'.$unicode.';">'.$name.' => &#x'.$unicode.';</option>';
}
}
In the array $emoji_unicode there is one entry (with 3 semi-column removed to not display emoji here):
'family_one_girl' => '1F468;‍👩‍👧',
For example: In order to make it works, I have to replace the line 'thinking_face' => '1F914', by 'thinking' => '1F914',
My question is: why ??
Thank you
Nop, the emoji text was changed by no code... I guess it was due to a wrong emoji file I used... I correct all the emoji manually and now I did not see the mismatch anymore...
If someone need the corrected file, I can provide it.
Related
In my controller, I access the comment data with $this->request->data['Comment']['text']. I use CakePHP's formhelper to build the form, and a plugin called Summernote to transform the textarea into a WYSIWYG editor. I save the comment as HTML in my database.
In this case, I am trying to submit a comment with just '>'
$data = $this->request->data['Comment']['text'];
pr($data);
//returns >
pr(mb_strlen($data, utf-8));
//returns 4
pr(mb_strlen('>', utf-8));
//returns 1
//that is the one that confuses me the most,
//it seems that there's a difference between $data and '>'
mb_detect_encoding($data);
//returns ASCII
I'm already using jQuery to check the number of characters entered on the front-end, so I can deactivate the submit-button when the user goes over the limit. This uses .innerText.length and works like a charm, but if I make that the only check people can just go into the element editor and re-enable the submit button to send however long comments they like.
EDIT:
var_dump($this->request->data['Comment']['text']) gave me the following result:
Note that unlike in the examples above, I am trying to send '>>>' here
array (size=1)
'text' => string '>>>' (length=12)
EDIT:
Alex_Tartan figured out the problem: I needed to do html_entity_decode() on my string before counting it with mb_strlen()!
I've tested the case here: https://3v4l.org/VLr9e
What might be the case is an untrimmed $data (white spaces won't show up in regular print - you can use var_dump($data)).
The textarea tag will enclose formatting spaces into the value.
Check out Why is textarea filled with mysterious white spaces?
so for that, you can do:
$data = '> ';
$data = trim($data);
// var_dump(data) will output:
// string(4) "> "
echo $data."\n";
//returns >
echo mb_strlen($data, 'UTF-8')."\n";
//returns 1
echo mb_strlen('>', 'UTF-8')."\n";
//returns 1
Update (from comments):
The problem was encoded html characters which needed to be decoded:
$data = html_entity_decode($data);
How will I convert all special characters to their corresponding html entity?
Special character would be like $ & / \ { } ( - ' , # etc.
I tried to use htmlentities() and htmlspecialchars(). But didn't solve my problem.
please check here. I want output like Entity Number i.e. column 3.
Actually the scenario is - I need to take input from fckeditor. and then save into the database. So I need to convert all special character to their corresponding html entity, from the text. Otherwise it's giving me error.
What you are looking is for an ASCII equivalent of a character. So you need to make use of ord().
By the way what divaka mentioned is right.
Do like this..
<?php
function getHTMLASCIIEquiv($val)
{
$arr=['$','&','/','\\','{','}','(','-','\'',',','#'];
$val = str_split($val);$str="";
foreach($val as $v)
{
if(in_array($v,$arr))
{
$str.="&#".ord($v).";";
}
else
{
$str.=$v;
}
}
return $str;
}
echo getHTMLASCIIEquiv('please check $100 & get email from test#cc.com');
OUTPUT :
please check $100 & get email from test#cc.com
Demo
I'm storing HTML content in a MySQL field with a text data type, and I'm using ckeditor as a WYSIWYG editor to create the HTML that is being stored in MySQL. I'm looking for a way for a user to put some kind of string that I could look for and replace with calling an include file. For example:
// This string contains the text pulled from mysql
$pageContent = "<p>This page contains a calendar of events</p> {{calendar}} <p>Choose a date or scroll through days to view events.</p>";
// Function needed that changes {{calendar}} to bring in my script calendar.php like include('calendar.php');
// Note that in this example I want to call my script that does the calendar stuff, but maybe I have a script to do a photo gallery which could be {{photogallery}}, or news {{news}}, or whatever...
// Print the $pageContent including the calendar.php contents here
print $pageContent;
Here's a little something that will take your text (in this case, $pageContent) and an array of parameters (ie array('calendar' => 'calendar.php')) and include the necessary file. It is currently untested, but should get you in the right direction.
function parseTemplate($templateText, $params)
{
foreach ($params as $key => $value)
{
ob_start();
include($value);
$includeContents = ob_get_contents();
ob_end_clean();
$templateText = str_replace('{{'.$key.'}}', $includeContents, $templateText);
}
return $templateText;
}
Usage in your case would be the following:
// This string contains the text pulled from mysql
$pageContent = "<p>This page contains a calendar of events</p> {{calendar}} <p>Choose a date or scroll through days to view events.</p>";
$params = array('calendar' => 'calendar.php');
$pageContent = parseTemplate($pageContent, $params);
// print the $pageContent including the calendar.php contents here
print $pageContent;
You could also use the same idea for simply replacing text instead of including files:
function parseTemplateText($templateText, $params)
{
foreach ($params as $key => $value)
{
$templateText = str_replace('{{'.$key.'}}', $value, $templateText);
}
return $templateText;
}
I am translating my website into different languages and I have over 130 pages so i want to pass my .php files through a function that will replace keywords
IE: Accessories = อุปกรณ์
Which is English to Thai.
I can get it to work using my method however... I have php (obviously) in these pages, and the output only displays the html and not executing the php
Is there a header method or something I have to pass at the start of my php pages..
here is the function I'm using to find text results and then replace them from my php files..
<?php
// lang.php
function get_lang($file)
{
// Include a language file
include 'lang_thai.php';
// Get the data from the HTML
$html = file_get_contents($file);
// Create an empty array for the language variables
$vars = array();
// Scroll through each variable
foreach($lang as $key => $value)
{
// Finds the array results in my lang_thai.php file (listed below)
$vars[$key] = $value;
}
// Finally convert the strings
$html = strtr($html, $vars);
// Return the data
echo $html;
}
?>
//This is the lang_thai.php file
<?php
$lang = array(
'Hot Items' => 'รายการสินค้า',
'Accessories' => 'อุปกรณ์'
);
?>
A lot of frameworks use a function to translate as it goes instead of replacing after the fact using .pot files. The function would look like this:
<h1><?php echo _('Hello, World') ?>!</h1>
So if it was English and not translated that function would just return the string untranslated. If it was to be translated then it would return the translated string.
If you want to continue with your route which is definitely faster to implement try this:
<?php
function translate($buffer) {
$translation = include ('lang_tai.php');
$keys = array_keys($translation);
$vals = array_values($translation);
return str_replace($keys, $vals, $buffer);
}
ob_start('translate');
// ... all of your html stuff
Your language file is:
<?php
return array(
'Hot Items' => 'รายการสินค้า',
'Accessories' => 'อุปกรณ์'
);
One cool thing is include can return values! So this is a good way to pass values from a file. Also the ob_start is an output buffer with a callback. So what happens is after you echo all of your html to the screen, right before it actually displays to the screen it passes all of that data to the translate function and we then translate all of the data!
I have a drupal template that renders some content like this:
<?php $field_collection_state_info = reset($content['field_state_information']['0']['entity']['field_collection_item']); ?>
<?php print render($field_collection_state_info['field_state_name']); ?>
but I would like to run the php ucfirst() so all rendered content is capitalized on the first letter.
I tried this:
<?php print ucfirst(render($field_collection_state_info['field_state_name'])); ?>
But that didn't work.
How should I achieve this?
Any advise is very much appreciated.
C
I think you will have to change the content of your render array before calling render():
// the 'und' part is depending on the translation of the node
$field_collection_state_info['field_state_name']['und'][0]['value'] =
ucfirst($field_collection_state_info['field_state_name']['und'][0]['value']);
// render
print render($field_collection_state_info['field_state_name']);
// or you could try to modify the safe_value part of the array depending on your
// filter settings for the field
$field_collection_state_info['field_state_name']['und'][0]['safe_value'] =
ucfirst($field_collection_state_info['field_state_name']['und'][0]['safe_value']);
// render
print render($field_collection_state_info['field_state_name']);
EDIT Try adding this into your template.php, it changes the value for a collection_field using hook_preprocess_node():
function YOURTHEME_preprocess_node(&$vars) {
if($vars['type'] == 'YOUR-CONTENT-TYPE') {
// you might need to replace the field_content_block with the name of your field <field_state_information>
$data = reset($vars['content']['field_content_block'][0]['entity']['field_collection_item']);
// turn the first character to uppercase
$data['field_text']['#object']->field_text['und'][0]['value'] = ucwords($data['field_text']['#object']->field_text['und'][0]['value']);
// set the data in the array, like I said above your might need to change the `field_collection_block` to `field_state_information`
$vars['content']['field_content_block'][0]['entity']['field_collection_item'] = $data;
}
}