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
Solved

str_replace() isn't working

Posted on 2006-11-18
14
216 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
14 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

840 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