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

str_replace and semicolons

Status
Not open for further replies.
Jun 19, 2001
86
0
0
US
I have the following function:

// convert characters to html
function convertstring($string) {
// variables
$search = array("~","`","!","@","#","\$","%","^","&","*","(",")","_","-","+","=","{",
"[","}","]","|","\\",":",";",'"',"'","<",",",">",".","?","/");
$replace = array("&#126;","&#96;","&#33;","&#64;","&#35;","&#36;","&#37;","&#94;",
"&#38;","&#42;","&#40;","&#41;","&#95;","&#45;","&#43;","&#61;","&#123;",
"&#91;","&#125;","&#93;","&#124;","&#92;","&#58;","&#59;","&#34;","&#39;",
"&#60;","&#44;","&#62;","&#46;","&#63;","&#47;");
return str_replace($search, $replace, $string);
}

If I pass it something like it returns:
http&#58&#59;&#47;&#47;or
http:;//
For some reason it is also converting the semi-colon in the first &#58; to &#59;. Any ideas why this is? If I remove the semi-colon from the search array it works correctly.

Thanks,
Nathan
 
Hi Nathan...

The why first -

You are sending the string ( through the loop and modifying it each time. Since you check for the semi-colon after you check for several other characters, it will convert all semi-colons (including semi-colons that were put in the string by your function from previous loops!)


i.e. -
Code:
iterations - output
===========================
0 - [URL unfurl="true"]http://www.tek-tips.com[/URL]
1 - [URL unfurl="true"]http://www.tek-tips.com[/URL]
.
.
.
14 - [URL unfurl="true"]http://www.tek&#45;tips.com[/URL]
.
.
23 - http&#58;//[URL unfurl="true"]www.tek&#45;tips.com[/URL]
24 - http&#58&#59;//[URL unfurl="true"]www.tek&#45&#59;tips.com[/URL] //<-- this is where it parses the semi-colons out
...

A simple fix would be to check for the semi-colon on the first iteration.


Jason
 
Jason,

Thanks for the reply! I understand the why now. If I move the semicolon to the first position, it fixes an example like The problem now is if I pass Hello; I get:

Hello&#38;#59;

I think the best thing would be to create a loop that works on a character in the string at a time.

Nathan
 
use strtr() instead

Code:
function convertstring($string) {
  // variables
  $search = array("~","`","!","@","#","\$","%","^","&","*","(",")","_","-","+","=","{",
"[","}","]","|","\\",":",";",'"',"'","<",",",">",".","?","/");
  $replace = array("&#126;","&#96;","&#33;","&#64;","&#35;","&#36;","&#37;","&#94;",
"&#38;","&#42;","&#40;","&#41;","&#95;","&#45;","&#43;","&#61;","&#123;",
"&#91;","&#125;","&#93;","&#124;","&#92;","&#58;","&#59;","&#34;","&#39;",
"&#60;","&#44;","&#62;","&#46;","&#63;","&#47;");
  return strtr($search, $replace, $string);
}
 
oops, i forgot to translate your arrays first. they must be in pairs; not to mention that the strtr function arguments are reversed...
Code:
<?php
function convertstring($string) {
	// variables
	$search = array("~","`","!","@","#","\$","%","^","&","*","(",")","_","-","+","=","{",
"[","}","]","|","\\",":",";",'"',"'","<",",",">",".","?","/");
	$replace = array("&#126;","&#96;","&#33;","&#64;","&#35;","&#36;","&#37;","&#94;",
"&#38;","&#42;","&#40;","&#41;","&#95;","&#45;","&#43;","&#61;","&#123;",
"&#91;","&#125;","&#93;","&#124;","&#92;","&#58;","&#59;","&#34;","&#39;",
"&#60;","&#44;","&#62;","&#46;","&#63;","&#47;");
	
	return strtr($string, convertArray($search, $replace));
}

function convertArray($search, $replace){
	$return = array();
	foreach ($search as $key=>$val){
		$return[$val] = $replace[$key];
	}
	return $return;
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top