Vim set file encoding

:set bomb
:set fileencoding=utf-8

Posted in Uncategorized | Tagged , | Leave a comment

MySQL regex update

Simple solution:

UPDATE  table
SET     words = REPLACE(words, 'brown', 'green')
WHERE   words REGEXP 'brown ' OR words REGEXP ' brown'

Better solution from http://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/:

SET sql_mode='NO_BACKSLASH_ESCAPES';

DELIMITER $$
CREATE FUNCTION  `regex_replace`(pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))

RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
 DECLARE temp VARCHAR(1000);
 DECLARE ch VARCHAR(1);
 DECLARE i INT;
 SET i = 1;
 SET temp = '';
 IF original REGEXP pattern THEN
  loop_label: LOOP
   IF i>CHAR_LENGTH(original) THEN
    LEAVE loop_label;
   END IF;
   SET ch = SUBSTRING(original,i,1);
   IF NOT ch REGEXP pattern THEN
    SET temp = CONCAT(temp,ch);
   ELSE
    SET temp = CONCAT(temp,replacement);
   END IF;
   SET i=i+1;
  END LOOP;
 ELSE
  SET temp = original;
 END IF;
 RETURN temp;
END$$
DELIMITER ;
Posted in MySQL | Leave a comment

Number format by country

Source: International Number Formats

Format Country
1,234.56 - Invariant Language (Invariant Country)
ar-SA – Arabic (Saudi Arabia)
zh-TW – Chinese (Taiwan)
en-US – English (United States)
he-IL – Hebrew (Israel)
ja-JP – Japanese (Japan)
ko-KR – Korean (Korea)
th-TH – Thai (Thailand)
ur-PK – Urdu (Islamic Republic of Pakistan)
hy-AM – Armenian (Armenia)
af-ZA – Afrikaans (South Africa)
hi-IN – Hindi (India)
sw-KE – Kiswahili (Kenya)
pa-IN – Punjabi (India)
gu-IN – Gujarati (India)
ta-IN – Tamil (India)
te-IN – Telugu (India)
kn-IN – Kannada (India)
mr-IN – Marathi (India)
sa-IN – Sanskrit (India)
kok-IN – Konkani (India)
syr-SY – Syriac (Syria)
dv-MV – Divehi (Maldives)
ar-IQ – Arabic (Iraq)
zh-CN – Chinese (People’s Republic of China)
en-GB – English (United Kingdom)
es-MX – Spanish (Mexico)
ar-EG – Arabic (Egypt)
zh-HK – Chinese (Hong Kong S.A.R.)
en-AU – English (Australia)
ar-LY – Arabic (Libya)
zh-SG – Chinese (Singapore)
en-CA – English (Canada)
es-GT – Spanish (Guatemala)
ar-DZ – Arabic (Algeria)
zh-MO – Chinese (Macao S.A.R.)
en-NZ – English (New Zealand)
ar-MA – Arabic (Morocco)
en-IE – English (Ireland)
es-PA – Spanish (Panama)
ar-TN – Arabic (Tunisia)
en-ZA – English (South Africa)
es-DO – Spanish (Dominican Republic)
ar-OM – Arabic (Oman)
en-JM – English (Jamaica)
ar-YE – Arabic (Yemen)
en-029 – English (Caribbean)
ar-SY – Arabic (Syria)
en-BZ – English (Belize)
es-PE – Spanish (Peru)
ar-JO – Arabic (Jordan)
en-TT – English (Trinidad and Tobago)
ar-LB – Arabic (Lebanon)
en-ZW – English (Zimbabwe)
ar-KW – Arabic (Kuwait)
en-PH – English (Republic of the Philippines)
ar-AE – Arabic (U.A.E.)
ar-BH – Arabic (Bahrain)
ar-QA – Arabic (Qatar)
es-SV – Spanish (El Salvador)
es-HN – Spanish (Honduras)
es-NI – Spanish (Nicaragua)
es-PR – Spanish (Puerto Rico)
zu-ZA – Zulu (South Africa)
xh-ZA – Xhosa (South Africa)
tn-ZA – Tswana (South Africa)
quz-PE – Quechua (Peru)
cy-GB – Welsh (United Kingdom)
fil-PH – Filipino (Philippines)
iu-Latn-CA – Inuktitut (Latin) (Canada)
mi-NZ – Maori (New Zealand)
ga-IE – Irish (Ireland)
moh-CA – Mohawk (Canada)
ns-ZA – Northern Sotho (South Africa)
mt-MT – Maltese (Malta)
1.234,56 ca-ES – Catalan (Catalan)
da-DK – Danish (Denmark)
de-DE – German (Germany)
el-GR – Greek (Greece)
is-IS – Icelandic (Iceland)
it-IT – Italian (Italy)
nl-NL – Dutch (Netherlands)
pt-BR – Portuguese (Brazil)
ro-RO – Romanian (Romania)
hr-HR – Croatian (Croatia)
sq-AL – Albanian (Albania)
sv-SE – Swedish (Sweden)
tr-TR – Turkish (Turkey)
id-ID – Indonesian (Indonesia)
sl-SI – Slovenian (Slovenia)
lt-LT – Lithuanian (Lithuania)
vi-VN – Vietnamese (Vietnam)
eu-ES – Basque (Basque)
mk-MK – Macedonian (Former Yugoslav Republic of Macedonia)
fo-FO – Faroese (Faroe Islands)
ms-MY – Malay (Malaysia)
gl-ES – Galician (Galician)
fr-BE – French (Belgium)
nl-BE – Dutch (Belgium)
pt-PT – Portuguese (Portugal)
sr-Latn-CS – Serbian (Latin, Serbia)
ms-BN – Malay (Brunei Darussalam)
de-AT – German (Austria)
es-ES – Spanish (Spain)
sr-Cyrl-CS – Serbian (Cyrillic, Serbia)
de-LU – German (Luxembourg)
es-CR – Spanish (Costa Rica)
es-VE – Spanish (Venezuela)
es-CO – Spanish (Colombia)
es-AR – Spanish (Argentina)
es-EC – Spanish (Ecuador)
es-CL – Spanish (Chile)
es-UY – Spanish (Uruguay)
es-PY – Spanish (Paraguay)
es-BO – Spanish (Bolivia)
sr-Cyrl-BA – Serbian (Cyrillic) (Bosnia and Herzegovina)
fy-NL – Frisian (Netherlands)
se-SE – Sami (Northern) (Sweden)
sma-SE – Sami (Southern) (Sweden)
hr-BA – Croatian (Bosnia and Herzegovina)
bs-Latn-BA – Bosnian (Bosnia and Herzegovina)
bs-Cyrl-BA – Bosnian (Cyrillic) (Bosnia and Herzegovina)
arn-CL – Mapudungun (Chile)
quz-EC – Quechua (Ecuador)
sr-Latn-BA – Serbian (Latin) (Bosnia and Herzegovina)
smj-SE – Sami (Lule) (Sweden)
quz-BO – Quechua (Bolivia)
1’234.56 de-CH – German (Switzerland)
it-CH – Italian (Switzerland)
fr-CH – French (Switzerland)
de-LI – German (Liechtenstein)
rm-CH – Romansh (Switzerland)
1 234,56 bg-BG – Bulgarian (Bulgaria)
cs-CZ – Czech (Czech Republic)
fi-FI – Finnish (Finland)
fr-FR – French (France)
hu-HU – Hungarian (Hungary)
nb-NO – Norwegian, Bokmål (Norway)
pl-PL – Polish (Poland)
ru-RU – Russian (Russia)
sk-SK – Slovak (Slovakia)
uk-UA – Ukrainian (Ukraine)
be-BY – Belarusian (Belarus)
lv-LV – Latvian (Latvia)
az-Latn-AZ – Azeri (Latin, Azerbaijan)
ka-GE – Georgian (Georgia)
uz-Latn-UZ – Uzbek (Latin, Uzbekistan)
tt-RU – Tatar (Russia)
mn-MN – Mongolian (Cyrillic, Mongolia)
nn-NO – Norwegian, Nynorsk (Norway)
sv-FI – Swedish (Finland)
az-Cyrl-AZ – Azeri (Cyrillic, Azerbaijan)
uz-Cyrl-UZ – Uzbek (Cyrillic, Uzbekistan)
fr-CA – French (Canada)
fr-LU – French (Luxembourg)
fr-MC – French (Principality of Monaco)
sma-NO – Sami (Southern) (Norway)
smn-FI – Sami (Inari) (Finland)
se-FI – Sami (Northern) (Finland)
sms-FI – Sami (Skolt) (Finland)
smj-NO – Sami (Lule) (Norway)
lb-LU – Luxembourgish (Luxembourg)
se-NO – Sami (Northern) (Norway)
1,234/56 fa-IR – Persian (Iran)
1 234-56 kk-KZ – Kazakh (Kazakhstan)
ky-KG – Kyrgyz (Kyrgyzstan)
1 234.56 et-EE – Estonian (Estonia)

Thanks to Igor Krupitsky.

Posted in General | Tagged | Leave a comment

Bash: getting confirmation to continue or exit

 

#!/bin/bash

while true
do
    read -r -p 'Are you sure? ' choice
    case "$choice" in
       y|Y)
           echo "Processing..."
           break
       ;;

       n|N)
           echo "Canceled."
           exit
      ;;
    esac
done

echo "Complete."


Posted in Linux | Tagged | Leave a comment

Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)

Currency Internationalization (i18n), Multiple Currencies and Foreign Exchange (FX)

Posted in web | Tagged | Leave a comment

Multibyte PHP Functions

Source: Using Multi-Byte Character Sets in PHP (Unicode, UTF-8, etc)

The following list details the PHP string functions which could cause problems when handling multi-byte strings. The multi-byte safe alternative is given when available:

mail() Try mb_send_mail() instead.
strlen() Try mb_strlen() instead.
strpos() Try mb_strpos() instead.
strrpos() Try mb_strrpos() instead.
substr() Try mb_substr() instead.
ereg() Try mb_ereg() instead.
eregi() Try mb_eregi() instead.
preg_match() Please investigate the /u option, as that provides UTF-8 awareness. The preg_* functions are contentious, because careful use can be safe. If you are unsure what to do, see mb_eregi() as a possible replacement.
preg_replace() Please investigate the /u option, as that provides UTF-8 awareness. The preg_* functions are contentious, because careful use can be safe. If you are unsure what to do, see mb_ereg_replace() as a possible replacement.
split() Try mb_split() instead.
explode() Try mb_split() instead.
stripos() Try mb_stripos() instead.
stristr() Try mb_stristr() instead.
strrchr() Try mb_strrchr() instead.
strripos() Try mb_strripos() instead.
strstr() Try mb_strstr() instead.
lcfirst() This function is flagged because its companion function (ucfirst) is not safe. However, this function is untested.
trim()rtrim() andltrim() May be multi-byte safe if you use UTF-8 only (multi-byte UTF-8 characters contain no byte sequences that resemble white space). Avoid UTF-16 & UTF-32, among others.
strip_tags() It may be multi-byte safe if you use UTF-8 only (multi-byte UTF-8 characters contain no byte sequences that resemble less-than or greater-than symbols). Avoid UTF-16 & UTF-32, among others.
Posted in PHP | Tagged , , | Leave a comment

Force secure browsing https

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} dir
RewriteRule ^(.*)$ https://www.example.com/dir/$1 [R,L]

Posted in Uncategorized | Tagged , | Leave a comment

Vim: set Caps Lock as Ctrl, set Pause as Caps Lock, let Esc be Esc

raymond@ubuntu:~$ cat .Xmodmap
remove Lock = Caps_Lock
remove Control = Control_L
keysym Caps_Lock = Control_L
keysym Pause = Caps_Lock
add Lock = Caps_Lock
add Control = Control_L

Posted in Linux | Tagged , , , | Leave a comment

PHP i18n using gettext

Localizing PHP web sites using gettext

Setup i18n gettext in your PHP application

Benchmarking PHP Localization – Is gettext fast enough?

Posted in Useful links | Tagged , | Leave a comment

Inserting a troublesome string from PHP to Javascript

<script type=”text/javascript”>
alert(“<?php echo preg_replace(“/\r?\n/”, “\\n”, addslashes($message)); ?>”);
</script>

 Passing variables to Javascript

Posted in PHP | Tagged | Leave a comment