• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

PHP - Help with str_replace

Hi, trying to use the following PHP code to replace a specific string in a variable with the contents of another variable. However the contents returned are blank.

 
$strYear = date("Y");
$strPageBaseText = str_replace("xxxYEARxxx", $strYear, $strPageBaseText);

Open in new window


If I change the code to use a fixed string rather than a variable, all is well.

$strPageBaseText = str_replace("xxxYEARxxx", "2011", $strPageBaseText);

Open in new window


Can anyone please tell me how to use str-replace to replace content based upon a variable rather than a fixed string.
0
Sean Clarke
Asked:
Sean Clarke
  • 2
1 Solution
 
Lukasz ChmielewskiCommented:
This works

<?php
$strPageBaseText = "xxxxxxxxxxxxYEARxxxxxxxxxxxxx";
$strYear = date("Y");
$strPageBaseText = str_replace("xxxYEARxxx", $strYear, $strPageBaseText);

echo "$strPageBaseText";
?>

Open in new window

0
 
Lukasz ChmielewskiCommented:
Do you want to replace this:
abcxxxYEARxxxabc
to
abc2011abc
?
0
 
hieloCommented:
>>$strPageBaseText = str_replace("xxxYEARxxx", $strYear, $strPageBaseText);
you will get a blank result if the right-most $strPageBaseText is initially blank. Make sure it is not blank BEFORE you attempt to do the replacing.

as for "how to use str_replace()", you are using it correctly.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now