I have a templating system for a current project. I use str_replace() to replace tokens within HTML documents with dynamic data.
For example, {%TEAM_NAME%} would be replaced with a name of a team taken from a table dependent on certain factors.
In one case the number of tokens (or string length of the search parameter) seems to have become so long it no longer replaces.
Here are snippets to show what I’m referring to:
$tokens = array('{%T_ID%}','{%NAV_BAR%}','{%TEAM_NAMES%}','{%CLUB_NAME%}','{%TEAM_NAME%}','{%STATE%}', '{%AGE_GROUP%}','{%LEVEL%}','{%CONTACT_NAME%}','{%CONTACT_ADDRESS%}','{%CONTACT_CITY%}','{%CONTACT_ZIP%}','{%CONTACT_EMAIL%}','{%CONTACT_PHONE_NUM%}','{%COACH_NAME%}','{%COACH_EMAIL%}','{%COACH_PHONE_NUM%}','{%SECRET_Q%}','{%SECRET_A%}','{%SO_CAL_ID%}','{%STATE_ID%}','{%NUM_PLAYERS%}','{%NOTES%}','{%SCORE_W%}','{%SCORE_L%}','{%SCORE_T%}','{%SCORE_GF%}','{%SCORE_GA%}','{%SCORE_GD%}','{%SCORE_POINTS%}');
$replace = array(
$tId => null,
$navBar->Parse() => null,
$teamNameOptions => null,
_CLUB_NAME_ => _TEAM_TABLE_,
_TEAM_NAME_ => _TEAM_TABLE_,
_STATE_ => _TEAM_TABLE_,
$ageOptions => null,
$levelOptions => null,
_CONTACT_NAME_ => _TEAM_TABLE_,
_CONTACT_ADDRESS_ => _TEAM_TABLE_,
_CITY_ => _TEAM_TABLE_,
_ZIP_ => _TEAM_TABLE_,
_CONTACT_EMAIL_ => _TEAM_TABLE_,
_CONTACT_PHONE_NUM_ => _TEAM_TABLE_,
_COACH_NAME_ => _TEAM_TABLE_,
_COACH_EMAIL_ => _TEAM_TABLE_,
_COACH_PHONE_NUM_ => _TEAM_TABLE_,
_SECRET_Q_ => _TEAM_TABLE_,
_SECRET_A_ => _TEAM_TABLE_,
_SO_CAL_ID_ => _TEAM_TABLE_,
_STATE_ID_ => _TEAM_TABLE_,
_NUM_PLAYERS_ => _TEAM_TABLE_,
_NOTES_ => _TEAM_TABLE_,
_SCORE_W_ => _TEAM_TABLE_,
_SCORE_L_ => _TEAM_TABLE_,
_SCORE_T_ => _TEAM_TABLE_,
_SCORE_GF_ => _TEAM_TABLE_,
_SCORE_GA_ => _TEAM_TABLE_,
_SCORE_POINTS_ => _TEAM_TABLE_
);
Doing some extensive white-box testing the values are all there, however the very last token and replacement {%SCORE_POINTS%} is not being replaced with a number as a should be, rather null.
This:
<input type="text" name="fScorePoints" value="{%SCORE_POINTS%}">
Should become:
<input type="text" name="fScorePoints" value="5">
But actually becomes:
<input type="text" name="fScorePoints" value="">
I tested further by adding another token and replacement and that too is not properly replaced, leading me to assume there is some form of character limit?
I can confirm all other tokens and replacements work perfectly fine, just not this last one.
What’s going on?
Thanks for any insight.
From the documentation for str_replace():
As the commenter pointed out, you have 1 less replacement value than you have subjects.