comports Posted January 5, 2007 Share Posted January 5, 2007 HI All,I need some help with a very small issue. I have some php code that reads stuff (text) from a txt file and then displays it on a page. All this is fine until I use the ' (apostrophe) character which then breaks the code. I guess it would be the same for " and $ etc..I have tried preceding it with a \ (backslash) but that did nothing to help.. Any clues.?ThanksAsh Quote Link to comment Share on other sites More sharing options...
Stooney Posted January 5, 2007 Share Posted January 5, 2007 try this:http://us2.php.net/manual/en/function.addslashes.php Quote Link to comment Share on other sites More sharing options...
comports Posted January 5, 2007 Author Share Posted January 5, 2007 Thanks Chris but I don't follow that.. In my text file have for example:[quote]Sometimes it works but sometimes it dont..[/quote]but I want it print out the "dont" as don't with the apostrophe. If i change my txt file to read "Sometimes it works but sometimes it don't." it breaks because of the '.Is there no simple way to include these and just display as a string. i.e. "don\'t" (even thought that don't work).ThanksAsh Quote Link to comment Share on other sites More sharing options...
Stooney Posted January 5, 2007 Share Posted January 5, 2007 [code]$string="Sometimes it works but sometimes it don't.";echo addslashes($string);echo $string; //will print Sometime it works but sometimes it don't.[/code] Quote Link to comment Share on other sites More sharing options...
taith Posted January 5, 2007 Share Posted January 5, 2007 code?basically... what you want to do, is when its coming out of the file, you want to $var=addslashes($var); this protects it from all them characters interfering with the codethen when displaying, you echo stripslashes($var); this takes all the slashes out, so you dont have to see them. Quote Link to comment Share on other sites More sharing options...
comports Posted January 5, 2007 Author Share Posted January 5, 2007 Thanks Again Chris.It seems to work if you "hard code" the string but I'm calling it from a text file.The only output I get is "Sometimes it works and sometimes it don\'t"My code is [code]$r_array=file("../cane-corso/images/text/".substr($files[$num],0,-4).'.txt'); echo $mQuotePath[0]; $query = $r_array[0]; parse_str($query); $string=$text; addslashes($string); echo $string;[/code]What am I doing wrong here.Thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 Try using the function [url=http://www.php.net/htmlentities]htmlentites()[/url] instead:[code]<?php$r_array=file("../cane-corso/images/text/".substr($files[$num],0,-4).'.txt');echo $mQuotePath[0];$query = $r_array[0]; parse_str($query); $string=$text;echo htmlentities($string,ENT_QUOTES);?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.