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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

str_replace() from MySQL Lookup Table

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I've created a table containing certain international characters along with the associated code (ie: 130 | é) so that I can automatically replace them in HTML content as it is being inserted into another table.

Code:
$result = mysql_query('SELECT CharacterName, CharacterCode FROM charactercodes');
   while ($row = mysql_fetch_row($result)) {
      $CharacterName = $row('CharacterName');
      $CharacterCode = "&#" . trim($row('CharacterCode')) . ";";
      $BodyText = str_replace($CharacterName, $CharacterCode, $BodyText);
	}

The variable $BodyText contains the HTML but for some reason that's probably obvious (though I'm not seeing it), the characters are being replaced by , or other characters that are not even in the table. This line:

$CharacterCode = "&#" . trim($row('CharacterCode')) . ";";

seems to be the culprit. I tried escaping the ; and & but then it inserts the escape character WITH the , or other character. What I am I doing wrong?

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
i believe your problem is you are () instead []

try
Code:
$row['CharacterName']

instead of
Code:
$row('CharacterName')
 
You're absolutely right but that's not the problem. I am using an IDE for site development and am not really using either

Code:
$row['CharacterName']

OR

Code:
$row('CharacterName')

but rather a built-in one that looks more like this

Code:
$DBconnection1->f("CharacterName")

I tried to give real-world PHP for the posting above but haven't done it in so long I had forgotten! No, the problem isn't there but thanks for letting me know of the error.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Hi,

1) If you assign the two columns to PHP variables and inspect them during the loop, do you get the results you would expect? That is, does the right code go with each name and do they both look like strings (of 1 character in the case of the name column)?

2) I believe that PHP has unusual rules for interpreting numbers - if there is a leading zero it is interpreted as a string, whilst with no leading zero, a decimal integer is assumed. Might this behaviour be affecting the result of assembling $CharacterCode?

 
This function enacts only when I save an entry through a form (and does other, more simple things than this too) but I've just modified it a bit save the results to a session that I can view using another script that I already have. Good idea and it does indeed show a mismatch!

The column with the Character Code does begin with zero in some cases but it is a varchar field and is being treated as such. That is, the code should simply be getting its value rather than evaluating it.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Is it possible that CharacterCode is some kind of reserved word? It is giving the oddest results. The column on the left, which is in part what the loop is inserting should be numbers, not characters. Some of these are not even in the table so it seems that it is somehow converting them to these odd codes which explains the problem:

Code:
Œ = = î
? = = ì
¤ = = ñ
¥ = = Ñ
Ò = = Ò
Ó = = Ó
Ô = = Ô
Õ = = Õ
¢ = = ó
£ = = ú
? = = ü
Å = = Å
† = = å

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
The solution turned out to be easy. I replaced this

Code:
$CharacterCode = "&#" . trim($row('CharacterCode')) . ";";
$BodyText = str_replace($CharacterName, $CharacterCode, $BodyText);

with this

Code:
$BodyText = str_replace($CharacterName, htmlentities(stripslashes($CharacterName)), $BodyText);

I had thought of using htmlentities() before but thought it would change < and > and other things it shouldn't. It only just occurred to me to do it only on those characters that were in my table. stripslashes() may not have been necessary but I added it just to be safe.

Don
Experienced in HTML, Perl, PHP, VBScript, PWS, IIS and Apache and MS-Access, MS-SQL, MySQL databases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top