By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

reversing str_replace() function in the textbox.

P: 3
I am developing my form into a captcha secured form and I used header() function if the user didn't put the verification code properly and still when the user go back to the form page the filled he have filled have still remained. In addition, users are usually submitting multiliner texts in the textbox so I used the str_replace() function to make all \n into %n% to prevent an error in the header function whenever the form is submitted. my script is:

Expand|Select|Wrap|Line Numbers
  1. // load the variables form address bar
  2. $category = $_REQUEST["jokecategory"];
  3. $title = $_REQUEST["joketitle"];
  4. $verif_box = $_REQUEST["verif_box"];
  5. $text = $_REQUEST["joke"];
  6.  
  7. // remove the backslashes that normally appears when entering " or '
  8. $MyTitle = addslashes($_POST[title1]);
  9. $MyText = addslashes($_POST[text1]);
  10.  
  11. $text = str_replace("\n", '%n%', $text);
  12.  
  13. // check to see if verificaton code was correct
  14. if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
  15.  
  16.     $q1 = "insert into dd_items set 
  17.  
  18.                     ItemTitle = '$MyTitle',
  19.  
  20.                     ItemCategory = '$MyCategory',
  21.  
  22.                     ItemSubcategory = '$MySubcategory',
  23.  
  24.                     ItemText = '$MyIn|$MyText',
  25.  
  26.                     Contributor = '$_SESSION[username]',
  27.  
  28.                     DateAdded = '$t' ";
  29.  
  30.  
  31.  
  32.     mysql_query($q1) or die(mysql_error());
  33.  
  34.  
  35.  
  36.     header("location:test.php");
  37.  
  38.     exit();
  39.  
  40. } else {
  41.     // if verification code was incorrect then return to contact page and show error
  42.     $text = str_replace("%n%", '\n', $text);
  43.     header("Location:".$_SERVER['HTTP_REFERER']."?jokecategory=$category&joketitle=$title&joke=$text&wrong_code=true");
  44.  
  45.     exit;
  46. }
  47.  
Now problem is that, I don't know where or how to reverse $text = str_replace("\n", '%n%', $text); function in the textbox so whenever the user entered a wrong verification code the \n will not appear neither the %n%, it will be automatically put in the next line inside the textbox. Any ideas? Please see the bolded lines, it is where I put the functions and tried to reverse it yet if was a failure.
Jul 31 '07 #1
Share this Question
Share on Google+
4 Replies


code green
Expert 100+
P: 1,726
PHP does not recognise \n within single quotes.
It needs to be within double quotes. "\n"
But instead of converting backwards and forwards why not store the original?
Also, by re-arranging your logic you could improve your efficiency more.
Jul 31 '07 #2

P: 3
What do you mean store the original? You mean I don't have to use the str_replace()? If that so, I will have an error "Warning: Header may not contain more than a single header, new line detected." if the form is submitted because the user may have entered more than one line in the textbox. That is why the solution I thought is to replace every "\n" into "%n" so that the server will not confused of the new lines. I said earlier I used the header() function. About re-arranging the logic, the script I posted is stripped just to make it simple to show my problem. :]

To see what I am doing, please click here . If the people entered more than one line in the textbox and able to provide the correct verification number, there's no problem at all but if the user entered a wrong verification number and has more that one line in the textbox, the "\n" actually in the textbox instead to make a new line.
I hope this makes clearer about my problem. :)
Jul 31 '07 #3

code green
Expert 100+
P: 1,726
What do you mean store the original?
[PHP]$origText = $text;
$text = str_replace("\n", '%n%', $text);[/PHP]
You mean I don't have to use the str_replace()?
Not twice no.
I am sorry but you lose me after this but the idea is you use $origtext with the error and $text when OK;
Jul 31 '07 #4

P: 3
I found the solution now! Wooohoo! What I did was to put this codes inside the textarea:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $text = $_GET['joke'];
  3. $text = str_replace("%n%", "\n", $text);
  4. echo "$text";
  5. ?>
  6.  
Thanks for the help guys! ^^
Jul 31 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.