Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

str_replace() isn't working

Posted on 2006-11-18
14
Medium Priority
?
222 Views
Last Modified: 2008-03-06
Ok, here's my problem:

$layout = $_POST["layout"];
$space = '"';
$mixed = $space . $layout . $space;

So in the form, $mixed would be $layout with quotes around. That isn't the problem.

$ttitle = '".$row["title"]."';
str_replace('TITLE' , $ttitle , $layout);

and when I type TITLE in the form, it just shows up "TITLE". What's happening?
0
Comment
Question by:spdaniel91
13 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17971623
You night need to use:

$layout = str_replace('TITLE' , $ttitle , $layout);

-r-
0
 
LVL 1

Author Comment

by:spdaniel91
ID: 17971635
but then, should it be

$layout = $_POST["layout"];
$layout = str_replace('TITLE' , $ttitle , $layout);
?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17971647
Yes, that is correct.

-r-
0
Technology Partners: 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!

 
LVL 6

Expert Comment

by:system_down
ID: 17971928
isn't it a typo?

I mean $ttitle is correct? shouldn't be $title with only one t?

Let me know.

S_D
0
 
LVL 1

Author Comment

by:spdaniel91
ID: 17972048
No, $ttitle is table title. Just a name I thought of ;)
0
 
LVL 1

Author Comment

by:spdaniel91
ID: 17972054
And what about if I want 3 more str_replace?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17972058
You just add additional statements:

$layout = $_POST["layout"];
$layout = str_replace('TITLE' , $ttitle , $layout);
$layout = str_replace('TITLE2' , $ttitle2 , $layout);
$layout = str_replace('TITLE3' , $ttitle3 , $layout);
$layout = str_replace('TITLE4' , $ttitle4 , $layout);

-r-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17972062
Or you use an array:

$replacements = array(
  'TITLE' => $ttitle,
  'TITLE2' => $ttitle2,
  'TITLE3' => $ttitle2
);

$layout = $_POST["layout"];
$layout = str_replace(array_keys($replacements), $replacements, $layout);

-r-
0
 
LVL 1

Author Comment

by:spdaniel91
ID: 17972845
Sorry guys, I found another way. But none of yours was what I wanted, so sorry ;)
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17973962
Hi,

What is the solution you chose to use?

-r-
0
 
LVL 1

Author Comment

by:spdaniel91
ID: 17974539

  /* Start replacing */
  $replace = array();
  $replace['title'] = str_replace("{TITLE}", $lrow['title'], $layout);   //Is the title
  $replace['date'] = str_replace("{DATE}", $lrow['date'] , $replace['title']); //Is the date
  $replace['main'] = str_replace("{MAIN}", $lrow['main'] , $replace['date']); //Is the content
  /* End replacing */

Yours was always with $layout in the end of str_replace(), but that echoed $layout 3 times.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17993996
No problems with PAQ-ing this question.
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18024955
PAQd, 70 points refunded.

DarthMod
CS Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

876 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