Jump to content


Photo

ereg_replace sucks


  • Please log in to reply
34 replies to this topic

#1 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 06:23 PM

I have been writing a script for over a month, and some parts of it were too hard so I paid for it to be done. But there's on thing that has consumed at least HALF of my time, and that's ereg_replace. Right now I am trying to do something relatively simple. Well at least it should be simple, but thinking I'd have the task completed in a couple of minutes - five ours later I am about to smash something:

I have a config file that contains this:

$headinclude = 'Head 'Info' Here';

and I have this in my script:

$headinclude = ereg_replace("\\\\'", "'", $headinclude);

If I don't have the above line in the script, a backslash gets placed before the '

So I changed it to:

$headinclude = ereg_replace('\\\\"', ''", $headinclude);

But then I can't have:

$headinclude = "Head "Info" Here";

I have been searching all over the fricken internet. Why isn't there a simple way for friggen quotes AND apostrophes to be used in forms?

If anyone knows a decent way to solve this, please let me know.

MrLeN





#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 06:41 PM

I'm not sure what you're really trying to do.

I don't understand because this is invalid PHP syntax to begin with:

$headinclude = 'Head 'Info' Here';


It has to be done one of these ways:

$headinclude = 'Head \'Info\' Here';


$headinclude = "Head 'Info' Here";


$headinclude = <<<ENDTAG
Head 'Info' Here
ENDTAG;


Similarly you can't have:

$headinclude = "Head "Info" Here";


It has to be done one of these ways:

$headinclude = "Head \"Info\" Here";


$headinclude = 'Head "Info" Here';


$headinclude = <<<ENDTAG
Head "Info" Here
ENDTAG;

If you got a file full of this type of thing:

$headinclude = 'Head 'Info' Here';

then fix it using your editors search and replace feature. I don't fully understand why you're trying to do this with regex.

hth.



#3 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 06:45 PM

$headinclude = 'Head 'Info' Here';

I know that is invalid. That's the probolem with using ereg replace.

The only way I can make that work is to make it:

$headinclude = 'Head "Info" Here';
or

$headinclude = "Head 'Info' Here";

But I can't do both, because I m ight want to use quotes and apostrophes:

$headinclude = "I might "want" to 'use' both";

either way, I can't win :(

MrLeN





#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 26 March 2006 - 06:48 PM

[a href=\"http://uk.php.net/stripslashes\" target=\"_blank\"]http://uk.php.net/stripslashes[/a]

stripslashes (opposite of addslashes) generally will sort out all your slash problems. use it on a backslashed string before you output it to the browser and voila

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 06:57 PM

You can use both by using backslashes.

$headinclude = "I might \"want\" to 'use' both";

Read up on these topics/functions:

[a href=\"http://us3.php.net/manual/en/function.get-magic-quotes-gpc.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.get-...-quotes-gpc.php[/a]

[a href=\"http://us3.php.net/manual/en/function.addslashes.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.addslashes.php[/a]

[a href=\"http://us3.php.net/manual/en/function.stripslashes.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.stripslashes.php[/a]

[a href=\"http://us3.php.net/manual/en/function.mysql-escape-string.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.mysq...cape-string.php[/a]

[a href=\"http://us3.php.net/manual/en/function.mysql-real-escape-string.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.mysq...cape-string.php[/a]

See FAQ page:

[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=31047\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=31047[/a]



#6 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 07:06 PM

I know I can use backslashes

I can't manually add backslashes, because the information is being entered in via a "form". And users aren't going to put slashes in. If I don't put ereg_replace code to remove the slashes it ends up with an extra slash every time it is submitted.

like \\\\\\\\\\\\\\\\\'s that.

The solution is to remove slashes. But then I can't use quotes and or apostrophes depending on if I use:

$headinclude = " This method"; //can't use " !!!

or

$headinclude = "This method"; //can't use ' !!!

MrLeN

#7 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 07:21 PM

Read up on the links provided. No need to use ereg_replace().

Real quick - steps for what to do when data is submitted from a form:

Determine if you have magic_quotes on or off.

When magic_quotes is on, stripslashes() on each field.

Validate each field (i.e. a number is numeric, a zip is a real zip, email address has correct syntax, etc.).

If any field doesn't pass validation, give error(s) and ask user to correct (put values entered back into form).

When each field passes validation: Do any other processing and BEFORE inserting/updating columns in a table, use addslashes() or mysql_escape_string() to each field.

hth.


#8 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 07:31 PM

I have magic_quotes on (already determined), and that is why I am using ereg_replace to strip the slashes.

When I strip slashes, then the code doesn't work anymore, because either " or ' needs a slash before it depending on whether I use

$this = ''
or
$this = ""

No matter what I do I can't win.

I have already read all the documents above. As I said I have been working on this for - well actually it's 6:29am, I have been trying to get this FRIGGEN form working since 10:00 last night. I'm totally pi$$ed off.

MrLeN





#9 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 07:43 PM

Effectively, all I am trying to do is prevent a form

\from
\\adding
\\\a
\\\\new
\\\\\slash
\\\\\\every
\\\\\\\time
\\\\\\\\it
\\\\\\\\\is
\\\\\\\\\\submitted

MrLeN



#10 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 07:52 PM

I have given you the answer in the steps outline, however, you might be too mad right now to notice the simple solution. I invite you to take a break and relax for a while. Then come back to it fresh.

You can always post relevant code snippets here so people can help you more precisely.


#11 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 07:58 PM

I have read and understood your answer(s). It doesn't help.

I have read those pages, more than once, and many more pages - I have gone 10 pages deep in Google.

I can't strip the slashes, because the form has to have at least one slash. I have tried all sorts of ereg_replace lines in all sorts of orders. I have rummaged through every php site and turorial I can find for 8 hours, and all I get are EROORS ERRORS ERRORS or slashes slashes slashes :(

I was hoping someone would say:

Oh is that all you want?

Just add this line:

$ladida wheee(all_fixed).

But apparently no one understands my problem or knows the answer :(

I hate it when I ask questions on forums and I get replies, but no answers. It's as if people get a kick out of giving cryptic resposes, just so that can say : "The answer is there - you must me stupid because you don't get it. I'm so smart".

MrLeN

#12 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 08:06 PM

What do you mean exactly by "...the form has to have at least one slash..."?

Post your current code (with the form).


#13 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 08:22 PM

I am trying to add the code, but I think it's too long. I just get a page saying you are not authorized to view this page - when I hit submit.

MrLeN

[a href=\"http://www.blueskytrain.com/code.txt\" target=\"_blank\"]http://www.blueskytrain.com/code.txt[/a]

The variable for $headinclude is in config.php

and it is like this:

$headinclude = "Form content winds up here"; //this is in the config.php

Now if someone enters a " then it causes an error.

So I tried:

$headinclude = 'Form content winds up here'; //this is in the config.php

but then I can't use a ' in the form.

Either way, I can't use a " or a '

I need to be able to use both.

MrLeN

#14 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 08:27 PM

Just relevant snippets, or split it into two posts, or give URL of form and code, or zip code and form and provide URL for download.

You forgot to answer my question. Are you saying users are allowed to enter slashes? back or forward slashes?


I see you changed your last post and gave the URL. Well, let me look at it now.



#15 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 08:28 PM

I added a text file. There us a link above.

MrLeN

#16 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 08:39 PM

Question: Do you absolutely need to have the config file in this format?

I would use a different approach. Like save the data in the file without the variable names. Then when you need to use it, you read the file data and assign each value to the appropriate variable.


#17 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 08:46 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
You forgot to answer my question. Are you saying users are allowed to enter slashes? back or forward slashes?
[/quote]

The form area I am trying to have editable is for a website header. So the user needs to add " or '

ie:

<a href="jacks-page.php">Jack's Page</a>

The script need not to have a coronary when either " or ' is used.

At this point, all I can do is get one or the other working. Not both.

All I can do is get \\\\\\\\\\\'this

or no slashes at all (by using stripslashes or ereg_replace) but I can't remove the slashes. I need at least one.

MrLeN

[!--quoteo(post=358639:date=Mar 26 2006, 03:39 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ Mar 26 2006, 03:39 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Question: Do you absolutely need to have the config file in this format?

I would use a different approach. Like save the data in the file without the variable names. Then when you need to use it, you read the file data and assign each value to the appropriate variable.
[/quote]

I have a configuration page where you can enter email, site name , different site colors etc.. and on that same page is a textarea to add header and footer code. It would be great if I could just get one problem fixed. It's one teeny weeny tiny problem, that has cost me 8 hours and if I can get it sorted, everything will be PERFECT and I'll be able to move on.

I have already thought about removing the header and footer textareas out of that form and making a different page. Where I will just use <?php include $headerfile ;?> and then make a page that writes to that headerfile, but I'd really like to just keep it as it is.

Surely there must me a way to add code that contains " and ' to a string, without causing code coronaries.

MrLeN



#18 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 09:21 PM

You simply put an include/require after the fclose.

require ("$file_dir/config.php");

or you need to stripslashes() before doing the echo with the htmlentities().




#19 MrLeN

MrLeN
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 26 March 2006 - 09:42 PM

"I don't know where New York is, how do I get there?"

"It's in America"

"I know that, but where in America?"

"Under Canada"

"Yeah, but where is New York in America?"

"Simple, just go to the Statue of Liberty or find Manhattan. That's where New York is.".

So deep down inside, what you're really trying to say; is that you don't know where New York is? You just like to look as though you know?

MrLeN

#20 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 26 March 2006 - 09:45 PM

[!--quoteo(post=358662:date=Mar 26 2006, 01:42 PM:name=MrLeN)--][div class=\'quotetop\']QUOTE(MrLeN @ Mar 26 2006, 01:42 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
"I don't know where New York is, how do I get there?"

"It's in America"

"I know that, but where in America?"

"Under Canada"

"Yeah, but where is New York in America?"

"Simple, just go to the Statue of Liberty or find Manhattan. That's where New York is.".

So deep down inside, what you're really trying to say; is that you don't know where New York is? You just like to look as though you know?

MrLeN
[/quote]
Please cut out the arrogance. Two people have told you the answer lies with stripslashes(). Have you bothered to even do what I have shared?





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users