Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replacing text in an array and using another function at the same time in PHP

I’m trying to use a function within an array that uses preg_replace() to replace certain commands

Below is the PHP code for observation

$traducoes = array(
    "Livro" => "Book",
    "Mesa" => "Table"
);
function translate($texto) { 
    global $traducoes;
    if($traducoes[$texto])    {
        return $traducoes[$texto];
    }    else     {
        return $texto;
    }
}

function codeReplace($string){
    $search = array(
        '/\(t\)\[(.*?)\]/',
        '/\[b\](.*?)\[\/b\]/',
        '/\[i\](.*?)\[\/i\]/',
        '/\[u\](.*?)\[\/u\]/',
        '/\[img\](.*?)\[\/img\]/',
        '/\[url\=(.*?)\](.*?)\[\/url\]/',
        '/\[code\](.*?)\[\/code\]/',
        '/\[color\=(.*?)\](.*?)\[\/color\]/'
    );
    $replace = array(
        translate("\\1"),
        '<strong>\\1</strong>',
        '<em>\\1</em>',
        '<u>\\1</u>',
        '<img src="\\1">',
        '<a href="\\1" target="_blank">\\2</a>',
        '<code>\\1</code>',
        '<span style="color:\\1;">\\2</span>'
    );
    $new = preg_replace($search, $replace, $string);
    return nl2br($new);
}


$string = 'Imprimindo [b]Negrito[/b] e [i]itálico[/i] e traduzindo (t)[Livro] na mesma frase';
echo codeReplace($string);

echo '<br><br>'.translate("Livro");

The result is this: https://i.stack.imgur.com/pCbuS.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I wanted the result to be this: https://i.stack.imgur.com/B9iSn.png

For some reason the function is not able to translate the text before making the substitution.

>Solution :

You’re only matching the first occurrence, not all occurrences in the string. So, instead of using preg_replace, use preg_replace_callback and include the translate function inside the preg_replace_callback method.

Here is some improvement tips.

  • Avoid Using Global Variables
  • Use isset() to Check Array Key Existence
  • Escaping Special Characters
  • Applying Translation on All Matches

Here’s the improved code.

$translateDictionary = array(
    "Livro" => "Book",
    "Mesa" => "Table"
);

function translate($text, $translateDictionary)
{
    if (isset($translateDictionary[$text])) {
        return $translateDictionary[$text];
    } else {
        return $text;
    }
}

function codeReplace($string, $traducoes)
{
    $search = array(
        '/\[b\](.*?)\[\/b\]/',
        '/\[i\](.*?)\[\/i\]/',
        '/\[u\](.*?)\[\/u\]/',
        '/\[img\](.*?)\[\/img\]/',
        '/\[url\=(.*?)\](.*?)\[\/url\]/',
        '/\[code\](.*?)\[\/code\]/',
        '/\[color\=(.*?)\](.*?)\[\/color\]/'
    );

    $replace = array(
        '<strong>$1</strong>',
        '<em>$1</em>',
        '<u>$1</u>',
        '<img src="$1">',
        '<a href="$1" target="_blank">$2</a>',
        '<code>$1</code>',
        '<span style="color:$1;">$2</span>'
    );

    $new = preg_replace($search, $replace, $string);

    // Now, perform translation for (t)[...] tags
    $new = preg_replace_callback('/\(t\)\[(.*?)\]/', function ($matches) use ($traducoes) {
        return translate($matches[1], $traducoes);
    }, $new);

    return nl2br($new);
}

$string = 'Imprimindo [b]Negrito[/b] e [i]itálico[/i] e traduzindo (t)[Livro] na mesma frase';
echo codeReplace($string, $translateDictionary);

I’ve made significant changes to the codeReplace method. Please review them.

The output of the code above:

Imprimindo <strong>Negrito</strong> e <em>itálico</em> e traduzindo Book na mesma frase
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading