Re: [PHP-DB] Search & Replace within PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



str_replace() is the first thing that comes to mind.  From the manual:

// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");


Using %keyword% and is kind of a template style method, but has worked out well for me (and I assume others) so try something along those lines.

You can also use arrays with str_replace to indicate a list of things that need replacing:

$template = "Please forward this to %NAME% at %ADDRESS%.  Call them at %PHONE% if there's a problem";
$tags = array("%NAME%", "%ADDRESS%", "%PHONE%");

$userinfoQY = "select Name, Address, Phone from users";
$userinfoRS = mysql_query($userinfoQY);
while($userinfoRW = mysql_fetch_assoc($userinfoRS)){
  $userinfo = array($userinfoRW['Name'], $userinfoRW['Address'], $userinfoRW['Phone']);

  echo str_replace($tags, $userinfo, $templte);
}


Is that the answer you were looking for?

-TG

= = = Original message = = =

Is there a search & replace PHP command?

An example I am needing this for would be:

My name is replace_with_real_name

I want to retrieve replace_with_real_name from a mySQL database and put in
the name ... the key is that I want to put the person's name in various
position ... so I want to search for something like "replace_with_real_name"
and be able to put it anywhere in the sentence.

Ron



___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux