Solved

replace string template in php

Posted on 2015-02-16
3
205 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 83

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 43

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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 33
Link Anchor Not working 9 43
Special characters in a TCPDF 4 23
Find RGB colors from a screen. 2 15
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

679 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