Jump to content


Photo

Help with array within preg_replace ...


  • Please log in to reply
2 replies to this topic

#1 Francky683

Francky683
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 August 2006 - 04:37 PM

Hey buds,

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

<?
	$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;
?>


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

Best regards,

Francis B.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 August 2006 - 04:51 PM

Try:
<?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;

?>


#3 Francky683

Francky683
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 03 August 2006 - 04:55 PM

Know what? I love you. Definately works.

Luv ya bud.

Francis B. <3




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users