473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Q: using str_replace to replace braket '{'?

From example in the book 'Programming PHP' (O'Reilly) (p.305)

$theKey = 'DESTINATION';
$target = '{' . $theKey . '}';
$inValues[$theKey] = 'some.php';

//$theTemplate = str_replace("\{$theKey}", $inValues[$theKey],
theTemplate);
//$theTemplate = str_replace("{$theKey}", $inValues[$theKey],
theTemplate);
$theTemplate = str_replace($target, $inValues[$theKey], $theTemplate);

--------------------------

First commented line is from book example but not working.
Second one is also not working too. It replaces string and left '{}'. So
result is '{some.php}'
Third one is my solution and it is working. It gives 'some.php'

But I think there is some way to just use str_replace alone.
Thank you

Sean Kim
Jan 10 '08 #1
1 1244
..oO(Sean Kim)
From example in the book 'Programming PHP' (O'Reilly) (p.305)

$theKey = 'DESTINATION';
$target = '{' . $theKey . '}';
$inValues[$theKey] = 'some.php';

//$theTemplate = str_replace("\{$theKey}", $inValues[$theKey],
theTemplate);
//$theTemplate = str_replace("{$theKey}", $inValues[$theKey],
theTemplate);
$theTemplate = str_replace($target, $inValues[$theKey], $theTemplate);

--------------------------

First commented line is from book example but not working.
In a double-quoted string the { cannot be escaped. Try this:

$theTemplate = str_replace('{'.$theKey.'}', $inValues[$theKey],
$theTemplate);

Micha
Jan 10 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fartsniff | last post by:
i found this code out in the ng, and its seems long and clunky, i am still experimenting with preg_match and _replace, but the syntax is a bit confusing. it seems i always misplace or mistype...
7
by: Jon | last post by:
Why does the following not replace the ":)"? str_replace(":)", "&nbsp;<img src=http://www.com.com/forum/emoticons/smiley.gif>&nbsp;", $_POST); But, the following does replace the ":("? ...
9
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row);...
2
by: mrquake99 | last post by:
Hi, I am looking for php tools / scripts with which I can convert php generated HTML to PDF. I was looking at FPDF and HTML2PDF (based on FPDF). However, HTML2PDF can not handle nested lists and...
9
by: Wayne | last post by:
$a = $_POST; # txt_content = This is a<CR><LF>Test $p = str_replace ("%0D%0A", "<br>", $a); That is the above code that I am using, however, it is not picking up the CR/LF from the textarea. I...
4
by: The Numerator | last post by:
I have this page (print.php). In that file, I want to to include a text file from "songs/breathe.txt", and replace all instances of "<b" with "<span" and "</b>" with "</span>". I know how I should...
3
by: robski | last post by:
Hello there, I am new to this forum and have done my best to look around for a solution to this problem. I am using PHP to generate an HTML Select drop down menu, with 4 options: function...
5
by: jmark | last post by:
I saw this example in php.net // Outputs: apearpearle pear $letters = array('a', 'p'); $fruit = array('apple', 'pear'); $text = 'a p'; $output = str_replace($letters, $fruit, $text);...
3
by: Confused but working on it | last post by:
So I've been trying to get a bit of code to: Read all of the files in a dir called thumbs, but not the . and .., use the filename in a link to get the same filename in an images dir. Now I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.