Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple String Replacements

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I have a function I wrote some time ago that replaces a bit of text in a paragraph and it's working well but I am trying to rework it to be able to make multiple replacements and cannot seem to get it to work. The replaced text is something like [VisitDate1], [VisitDate2] etc. and here is the original function with the $Location variable being an integer that references the ID in the database table:

PHP:
function visitDates($Location, $PageContent) {
	// Get Oregon visit dates
	$sqlDates = "SELECT StartDate, EndDate FROM locations WHERE ID = $Location"; 
	$rowDates = DBConnect($sqlDates, "Select", "geoip");

	$StartDate = date("F j", $rowDates['StartDate']);
	$EndDate = date("F j, Y", $rowDates['EndDate']);
	$ReplaceText = "[VisitDate$Location]";
	if ($rowDates['StartDate'] > time()) :
		$ReplaceDate = "<em>The next visit date is from $StartDate to $EndDate</em>";
	else:
		$ReplaceDate = "No future dates have been scheduled yet but be sure to check back soon!";
	endif;
	echo str_replace($ReplaceText, $ReplaceDate, $PageContent);
}

Here is the new version which is no longer getting the $Location variable and instead it is being built dynamically from the entries. It is building an array of all the possible [VisitDateN] values along with an array of the text to replace it. It is not producing any errors but neither is the text being replaced so clearly I've missed something basic here. Can anyone advise?

PHP:
function visitDates($PageContent) {
	global $VisitDates;
	global $Replacements;
	$sqlDates = "SELECT ID, Location, StartDate, EndDate FROM locations"; 
	$rowDates = DBConnect($sqlDates, "Multiple", "geoip");

	for ($i=0;$i<=count($rowDates)-1;$i++) :
		$ID = $rowDates[$i]['ID'];
		$VisitLocation = $rowDates[$i]['Location'];	
		$StartDate = date("F j", $rowDates[$i]['StartDate']);
		$EndDate = date("F j, Y", $rowDates[$i]['EndDate']);

		if ($rowDates[$i]['StartDate'] > time()) :
			$VisitText = sprintf('"<em>The next visit date in %s is from %s to %s</em>"',
												$VisitLocation,
												$StartDate,
												$EndDate);
		else:
			$VisitText = sprintf('"No future dates to %s have been scheduled yet but be sure to check back soon!"',
												$VisitLocation);
		endif;

		$Replacements .= "\"[VisitDate$ID]\",";
		$VisitDates .= "$VisitText,";
	endfor;

	$VisitDates = rtrim($VisitDates,",");
	$VisitDates = array($VisitDates);
	$Replacements = rtrim($Replacements,",");
	$Replacements = array($Replacements);

	echo str_replace($Replacements, $VisitDates, $PageContent);
}
 
You are building the replacement list dynamically, but how is your $PageContent getting that list?

ie. $Replacement string needs to exists identically in $PageContent for the replacement to happen.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
$PageContent isn't getting the list as such. It is simply a variable that contains the string as its content and is being passed into str_replace() as the third value. As for as str_replace() is concerned, $PageContent is just a string of text. Passing the first two values as arrays into str_replace should cause any matches to be replaced. In other words, it should not work any differently than it was when only a single value was being replaced but perhaps I do not totally understand the question or confusion.
 
It needs to find the string you built exactly as you built it, in the $PageContent variable. Down to spaces, and commas. Everything must be the same. So yes, $PageContent is very related.

So if your built string ends up being:"[VisitDate1], [VisitDate2], [VisitDate3]", then it needs to find exactly that string in $PageContent, all together. Is it there?









----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
No, that is not what it should be doing. If [VisitDate1] OR [VisitDate2] OR [VisitDate3] etc. are found in $PageContent, they should be replaced. I think that's part of the problem, that it is not building a proper array from the values. It only resembles an array but apparently all the values are together as one value rather than being individual.
 
I think I see the problem with the code but I've not worked out exactly what to do about it. I suspect the problem is that the coding isn't building proper arrays. When I view a print_r($Replacements), which contains the strings to be replaced, the result looks a bit odd. Using this:

PHP:
echo "<pre>";
print_r($Replacements);
echo "</pre>";

. . . gives this result:

Code:
Array
(
    [0] => [VisitDate1],[VisitDate2],[VisitDate3],[VisitDate4],[VisitDate5]
)

. . . when it should look like this:

Code:
Array
(
    [0] => [VisitDate1]
    [1] => [VisitDate2]
    [2] => [VisitDate3]
    [3] => [VisitDate4]
    [4] => [VisitDate5]
)
 
Got it and it was, as I thought, simple. I had missed the [] when trying to create the array and, in fact, I was trying to build the arrays in a unnecessary and round-about way. So replacing this:


PHP:
$Replacements .= "\"[VisitDate$ID]\",";
$VisitDates .= "$VisitText,";

. . . with this:

PHP:
$Replacements[bold][][/bold] = "[VisitDate$ID]";
$VisitDates[bold][][/bold] = $VisitText;

. . . and removing this entirely:

PHP:
$VisitDates = rtrim($VisitDates,",");
$VisitDates = array($VisitDates);
$Replacements = rtrim($Replacements,",");
$Replacements = array($Replacements);

did the trick and it works perfectly now.
 
Ahh, I see, you never mentioned you were creating an array, and it was not apparent from the code.

Also its important to always echo out your dynamically built variables before trying to use them. Just so you know they are what you expect them to be.

Anywya, glad you worked it out.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I did echo them and thought the array looked a bit odd! That's how I discovered the problem. Actually I did say in my original question that they were an array but no matter, it's solved now. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top