Solved

replace string template in php

Posted on 2015-02-16
3
201 Views
Last Modified: 2015-02-22
Hi ,
In php I have a returned string as
$message='Enter message[phone][lastname][lastname][firstname]'

How do I replace the  [ ] with actual values like
$phone='424234' and needs to go where [phone] is;

  $phone='4444';
   str_replace('[phone]',$phone,$template) ;
'didnt work'
 and so the message will now have the data values inserted?
0
Comment
Question by:jagguy
3 Comments
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40613790
Here is a demo page that shows how to do it.  It is self-contained, it POSTs to itself.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Template Replace</title>
</head>
<body>
<h1>Template Replace</h1>
<?php 
echo $message='Enter message [phone] [lastname] [firstname]<br>';
if (!isset($_POST["phone"]))  $phone = ''; else $phone = substr($_POST["phone"],0,64);
if (!isset($_POST["lastname"]))  $lastname = ''; else $lastname = substr($_POST["lastname"],0,64);
if (!isset($_POST["firstname"]))  $firstname = ''; else $firstname = substr($_POST["firstname"],0,64);
if (!isset($_POST["submit"]))  $submit = ''; else $submit = substr($_POST["submit"],0,64);

$msg2 = $message;
$msg2 = str_replace('[phone]',$phone,$msg2);
$msg2 = str_replace('[lastname]',$lastname,$msg2);
$msg2 = str_replace('[firstname]',$firstname,$msg2);

echo $msg2."<br>";
?>
<form action="#" method="post">
<table border="0" cellpadding="0" cellspacing="0" summary="">
<tr><td>phone</td>
<td>
<input type="text" name="phone" value="4444" />
</td></tr>
<tr><td>lastname</td>
<td>
<input type="text" name="lastname" value="lastname" />
</td></tr>
<tr><td>firstname</td>
<td>
<input type="text" name="firstname" value="firstname" />
</td></tr>
<tr><td>&nbsp;</td>
<td>
<input type="submit" name="submit" value="Submit" />
</td></tr>
</table>
</form>
</body>
</html>

Open in new window

0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 40613877
Here's an option using regular expression (preg_replace):

<?php
$message = 'Enter message [phone] [lastname] [firstname]';

$patterns = array(
	'/\[phone\]/',
	'/\[lastname\]/',
	'/\[firstname\]/'
);

$replacements = array(
	'123456',
	'stanyon',
	'chris'
);

echo preg_replace($patterns, $replacements, $message);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40613919
It looks like you're dealing with string data.  This is good background information:
http://php.net/manual/en/language.types.string.php#language.types.string.details

PHP has HEREDOC notation that is useful for templates.  Heed, but do not be put off by the warning on the man page.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

To see how it works, follow the $name and $phone variables in this script.
http://iconoun.com/demo/temp_jagguy.php

<?php // demo/temp_jagguy.php

/**
 * See: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28618134.html
 */
ini_set('display_errors', TRUE);
ini_set('log_errors',     TRUE);
error_reporting(E_ALL);


// A DATA SET THAT WE NEED IN OUR OUTPUT STRING
$name  = 'JagGuy';
$phone = '424234';

// DEMONSTRATE THE USE OF HEREDOC TEMPLATES
$template = <<<EOD
<p>
This is a message to $name<br>
The phone $phone is here!
</p>
EOD;

// SHOW THE TEMPLATE WITH THE INCLUDED DATA SET
echo $template;

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now