Jump to content

Help with array within preg_replace ...


Francky683

Recommended Posts

Hey buds,

I'm stuck on a problem with a preg_replace line, here it is :

[code=php:0]<?
$text = "[flag]usa[/flag]";
$flagSize = "18-12";
$flagArray['usa'] = 'United States';
$outputText = preg_replace("/\[flag\]([a-z]{3})\[\/flag\]/", "<img src=\"images/flags/$flagSize/$1.gif\" class=\"flag\" alt=\"$flagArray[$1]\">", $text);
echo $outputText;
?>[/code]


If somehow doesn't accept the array within the function, anyone?

Best regards,

Francis B.
Link to comment
https://forums.phpfreaks.com/topic/16465-help-with-array-within-preg_replace/
Share on other sites

Try:
[code=php:0]<?php

$text = "[flag]usa[/flag]";
$flagSize = "18-12";
$flagArray['usa'] = 'United States';

function getFlag($flag)
{
    global $flagSize, $flagArray;

    $flag = '<img src="images/flags/' . $flagSize . '/' . $flag . '.gif" class="flag" alt="' . $flagArray[$flag] . '">';

    return $flag;
}

$outputText = preg_replace("/\[flag\]([a-z]{3})\[\/flag\]/e", "getFlag(\"$1\")", $text);

echo $outputText;

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.