Jump to content

Archived

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

Francky683

Help with array within preg_replace ...

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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.