Solved

replace string template in php

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

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn how to dynamically set the form action using jQuery.
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…

861 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