?
Solved

replace string template in php

Posted on 2015-02-16
3
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 2000 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 111

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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

743 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