Re: Re: replace value of array by key of array in string variable with pre

From: Date: Sat, 21 Aug 2004 18:44:17 +0000
Subject: Re: Re: replace value of array by key of array in string variable with pre
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-194404@lists.php.net to get a copy of this message
On Sat, 21 Aug 2004 12:54:15 -0500, Shawn McKenzie <nospam@mckenzies.net> wrote: > Turbo wrote: > > Hi, > > > > I have array variable and string variable. > > I want to replace value of array by key of array in string variable > > with preg_replace(). > > > > Example : > > $message=array( > > 'name'=>'My Computer', > > 'version'=>'1.0' > > ); > > $strValue="I am $name,build version $version\n"; > > > > How's to replace? You could just change your string to be: $strValue = 'I am '.$message['name'].',build version '.$message['version']."\n"; or $output = preg_replace('/\$([A-Z_\-0-9]*)/ie', '$message["$1"]', $strValue); This is not quite a complete solution and may not work (is not tested). > > > > Yingyos > > Why preg_replace? > > Just do extract($message); then you have vars from the keys: > > $name ='My Computer'; > and > $version = '1.0'; > > Then those vars are used in the $strValue var. > -- DB_DataObject_FormBuilder - The database at your fingertips http://pear.php.net/package/DB_DataObject_FormBuilder paperCrane --Justin Patrin--

« previous php.general (#194404) next »