0

I have a problem with this code...

$text = "cat cow";
$a = array("cat", "cow");
$b = array("dog", "bull");
$c = array_combine($a, $b);
$output = strtr($text, $c);

This code merges the two arrays and replaces text. This is working fine but when I try this code with unicode chars... i.e.

<?php

function convert($fromc, $toc, $otext) {
    $raavi = array("~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_", "=", "ਥ", "ੱ", "ਓ", "੍ਰ", "ਠ", "ੈ", "ੂ", "ੀ", "ੌ", "ਫ", "{", "}", "।", "ਤ", "ੱ", "ੲ", "ਰ", "ਟ", "ੇ", "ੁ", "ਿ", "ੋ", "ਪ", ".", "॥", "\\", "ੳ", "ੰ", "ਧ", "ਾਂ", "ਘ", "੍ਹ", "ਝ", "ਖ", "਼", ":", "\"", "ਅ", "ਸ", "ਦ", "ਾ", "ਗ", "ਹ", "ਜ", "ਕ", "ਲ", "   ;", "'", "ਢ", "ਯ", "ਛ", "ੜ", "ਭ", "ਂ", "ੰ", ",", ">", "?", "ਡ", "ਣ", "ਚ", "ਵ", "ਬ", "ਨ", "ਮ", ",", "।", "/", "ੴ", "॥", "#", "੍ਰ", "•", "।", "ੴ", "ੴ", "☬", "ਸ਼", "ਖ਼", "ਗ਼", "ਜ਼", "ਫ਼", "੍ਯ", "#", "॥", "ਲ਼", "੧", "੨");
    $drchatrik = array("~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "|", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "Z", "X", "C", "V", "B", "N", "M", ",", ">", "?", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "¡", "«", "¬", "R", "•", "»", "Ã", "Å", "Ç", "È", "É", "Ê", "Ë", "Ì", "Î", "Ñ", "Ò", "Ü", "ñ", "ò");
    $combine = array_combine($$fromc, $$toc);
    $converted = strtr($otext, $combine);
    echo ($converted);
}

?>

It doesn't work. It combines the arrays when I echo it with print_r but doesn't replace text.

Plzz help..

Shahbaz Singh
  • 191
  • 1
  • 9
  • `$otext` is defined in the function.. actually I have made this code a function... – Shahbaz Singh Jul 13 '12 at 06:19
  • why do you need `$raavi` and `$drchatrik` if you're not using them ? and what's with the double `$$` (`$$fromc, $$toc`) ? – Nir Alfasi Jul 13 '12 at 06:23
  • actually the function will be executed something like this.. `convert("raavi", "drchatrik", "text here")` where raavi and drchatrik are variable names – Shahbaz Singh Jul 13 '12 at 06:26

3 Answers3

0

You can try using mb_strtr from here http://code.google.com/p/mbfunctions/

Also please provide sample parameters for convert function not working.

0

I'ts bit difficult to understand exactly what you want but I would use a loop to iterate over the array

$raavi = array("ਥ", "ੱ");//shortened for ease of reading
$drchatrik = array("Q", "W");

function convert($otext) {
    global $raavi, $drchatrik
    return(trim(preg_replace($raavi, $drchatrik, $otext)));
}

foreach(array_combine($fromc, $toc) as $otext)
{
    $text_out[] = convert($otext)
}

print_r($text_out);
Andy Gee
  • 3,149
  • 2
  • 29
  • 44
0

You have your from an to mixed up, if you call it like convert("drchatrik", "raavi", "text here") it will work

Musa
  • 96,336
  • 17
  • 118
  • 137
  • @ShahbazSingh does `convert("raavi", "drchatrik", "ਟੲਣਟ ਹੲਰੲ")` give you `text here` – Musa Jul 13 '12 at 06:58