Solved

replace string template in php

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - Powershell 7 80
Sending HTML Formatted Email from ASP.NET 2 53
Trying to use &NBSP to line up 3 rows 12 38
Considerations PHP using multiple locations 9 33
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.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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).

739 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