Jump to content

preg_replace with a " as replace


mort

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.