Jump to content

str_replace problem


DarkElf

Recommended Posts

I've been working on a basic content management system for a while, so far it as simply been an online interface which users can use to directly alter the html of the pages. I'm now trying to make it a bit cleverer, the idea is that text for a certain area of the site is taken from a txt file, the user modifies the txt file using the interface. The back end bit works fine but I'm having problems reproducing the data at the front end.If the user only wants one paragraph then there is no problem, you could just use the code:

<p><?php //include contentsinclude ('content.txt');?></p>

But if the user wants to include new lines or paragraphs it has to be a bit more complicated. Instead of including the file you need to open it and write it to a string and then perform the string replace function to replace all occurances of "\n" (php's delimiter for a new line) with "<br />". Here is my attempt:

 <p><?php //Open content file$fp = fopen ('content.txt','r');//Write contentswhile (!feof($fp))	{	$print = fgets($fp, 999999);	//add <br /> tags	$print = str_replace("\n","<br />",$print);	echo $print;	}fclose($fp);?></p>

All nice in theory, except it doesn't work. When it gets to a new line it puts in the "<br />" tag and then stops reading the file. If the content.txt file is this:

Test line oneTest line two

the output on my page is this:

<p>Test line one<br /></p>

What is really annoying me is that at one point I managed to get it to work perfectly, then I tried adding a few extra lines of code for some further functionality and it stopped working! I've taken those lines back out again but I've obviously missed something as it no longer behaves how it did before! Any ideas?

Link to comment
Share on other sites

ok, here's a new predicament (but part of the same problem).I've been dealing with reserved characters, for instance "<" and ">". Obviously if the user were to incorporate those into their text it would cause some problems, therefore when the back-end writes the file it's replacing each of those (and some others) with the relevant alias. That bit works fine, but what I've now spotted is a similar kind of problem with backslashes.Because of the magic quotes feature (grrrrr) i've had to perform stripslashes on the string before it is written to the file. If the user incorporates backslashes though they are unfortunately lost :) I know I can disable magic quotes by putting a flag in the .htaccess file, in which case I could remove the stripslashes function, but I'm trying to avoid that so as to maximise the ease of portability of my code.Any ideas as to how I can get around this?

Link to comment
Share on other sites

Can you pinpoint where you are losing the slashes?

<html><?phpecho phpversion();if(isset($_POST['t1'])){$tStr = $_POST['t1'];echo "<p>$tStr</p>";$tStr = stripslashes($tStr);echo "<p>$tStr</p>";$tStr = stripslashes($tStr);echo "<p>$tStr</p>";}?><form id="f1" action="" method="post"><textarea name="t1" rows="8" cols="40">This is a "quoted" string with a'quoted' phrase with a \ for more \ testing.</textarea><input type="submit"></form></html>

The above test page yields the following results( ver 4.3.7 ):

This is a \"quoted\" string with a \'quoted\' phrase with a \\ for more \\ testing. This is a "quoted" string with a 'quoted' phrase with a \ for more \ testing. This is a "quoted" string with a 'quoted' phrase with a for more testing.

-hs

Link to comment
Share on other sites

I'm not seeing entirely how that helps?I do have a kind of idea though. The script at the minute takes the string passed to it, strips slashes, and writes it to a file which is later opened by another page to display the content. The strip slashes is required because on some servers the magic quotes function might be enabled (and i want the script to be easily portable). If magic quotes is enabled it works fine because this escapes user inputted slashes, the problem occurs when its on servers where magic quotes isn't enabled because it strips user inputted slashes which haven't been escaped by magic quotes.I think what i need is some code to test the server to see if it is running magic quotes and then it can decide to perform stripslashes if required. Is there a way I can do this?

Link to comment
Share on other sites

In your post you stated that using stripslashes was removing your slashes.

Because of the magic quotes feature (grrrrr) i've had to perform stripslashes on the string before it is written to the file. If the user incorporates backslashes though they are unfortunately lost
My post was meant to illustrate that you would only be missing the slashes if you filtered the string more than necessary.get_magic_quotes_gpc() will return 1 if on
  if(get_magic_quotes_gpc())  $temp = stripslashes($temp);

-hs

Edited by hacknsack
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...