mort Posted April 4, 2007 Share Posted April 4, 2007 Hey peeps Am trying to turn an HTML file into a string (will use file_get_contents()), and then turn all of the "s into \"s so that I can echo the string. Having problems with what to put in the pattern and what t put in replace I have $file = file_get_contents("file.html"); $pattern = "\""; $replacement = "\\\""; echo preg_replace($pattern, $replacement, $file); I don't get an output at all, anyone tell me *** i'm doing wrong? Its probably something simple lol :cool: Link to comment https://forums.phpfreaks.com/topic/45614-preg_replace-with-a-as-replace/ Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 You don't want to do that at all, what you want to do is use the htmlentities() function with the "ENT_QUOTES" option: <?php $file = file_get_contents("file.html"); echo htmlentities($file,ENT_QUOTES); ?> Ken Link to comment https://forums.phpfreaks.com/topic/45614-preg_replace-with-a-as-replace/#findComment-221529 Share on other sites More sharing options...
mort Posted April 4, 2007 Author Share Posted April 4, 2007 ahh cool can i use that to literally \ each " and then output it as \"? as I want to edit the code, stick variables in, and have it all as one massive concatinated string like so echo "<tr>" ."<td background=\"$images_dir/index_04.gif\" width=\"562\" height=\"628\" alt=\"News\" valign=\"top\"><a name=\"news\"></a></td>" ."</tr>" ."<tr>" ."<td background=\"$images_dir/index_05.gif\" width=\"562\" height=\"361\" alt=\"Technical Info\" valign=\"top\"><a name=\"tech\"></a></td>" ."</tr>" ."<tr>" ."<td background=\"$images_dir/index_06.gif\" width=\"562\" height=\"913\" alt=\"Dealer Spotlight\" valign=\"top\"><a name=\"dealer\"></a></td>" ."</tr>"; So rather than manually replacing each " with \", I wanted to make a script to do it for me Link to comment https://forums.phpfreaks.com/topic/45614-preg_replace-with-a-as-replace/#findComment-221543 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 The function htmlentities() does not escape quotes, it converts them to the HTML Entity form like " so when they are echoed to the screen they look correct. If you want to escape the quotes, use the addslashes() function. Ken Link to comment https://forums.phpfreaks.com/topic/45614-preg_replace-with-a-as-replace/#findComment-221548 Share on other sites More sharing options...
mort Posted April 4, 2007 Author Share Posted April 4, 2007 lol ure a star dude i always try to over complicate things, i use addslashes() all the time so dont know why I didnt think of it cheers anyways Link to comment https://forums.phpfreaks.com/topic/45614-preg_replace-with-a-as-replace/#findComment-221553 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.