×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Multiple String Replacements

Multiple String Replacements

Multiple String Replacements

(OP)
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:

CODE --> 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?

CODE --> 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);
} 

RE: Multiple String Replacements

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

RE: Multiple String Replacements

(OP)
$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.

RE: Multiple String Replacements

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

RE: Multiple String Replacements

(OP)
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.

RE: Multiple String Replacements

(OP)
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:

CODE --> PHP

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

. . . gives this result:

CODE --> ARRAY

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

. . . when it should look like this:

CODE --> ARRAY

Array
(
    [0] => [VisitDate1]
    [1] => [VisitDate2]
    [2] => [VisitDate3]
    [3] => [VisitDate4]
    [4] => [VisitDate5]
) 

RE: Multiple String Replacements

(OP)
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:


CODE --> PHP

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

. . . with this:

CODE --> PHP

$Replacements[] = "[VisitDate$ID]";
$VisitDates[] = $VisitText; 

. . . and removing this entirely:

CODE --> PHP

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

did the trick and it works perfectly now.

RE: Multiple String Replacements

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

RE: Multiple String Replacements

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close