#### Archived

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

# ereg_replace sucks

## Recommended Posts

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:

[code]
$headinclude = 'Head 'Info' Here'; [/code] and I have this in my script: [code]$headinclude = ereg_replace("\\\\'", "'", $headinclude); [/code] If I don't have the above line in the script, a backslash gets placed before the ' So I changed it to: [code]$headinclude = ereg_replace('\\\\"', ''", $headinclude); [/code] But then I can't have: [code]$headinclude = "Head "Info" Here";
[/code]

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

##### Share on other sites
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
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
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. #### Share this post ##### Link to post ##### Share on other sites$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 #### Share this post ##### Link to post ##### Share on other sites [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 #### Share this post ##### Link to post ##### Share on other sites 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.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]

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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 pied off.

MrLeN

##### Share on other sites
Effectively, all I am trying to do is prevent a form

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

MrLeN

##### Share on other sites
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.

##### Share on other sites
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 #### Share this post ##### Link to post ##### Share on other sites What do you mean exactly by "...the form has to have at least one slash..."? Post your current code (with the form). #### Share this post ##### Link to post ##### Share on other sites 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:

[code]
$headinclude = "Form content winds up here"; //this is in the config.php [/code] Now if someone enters a " then it causes an error. So I tried: [code]$headinclude = 'Form content winds up here'; //this is in the config.php
[/code]

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

##### Share on other sites
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.

##### Share on other sites
I added a text file. There us a link above.

MrLeN

##### Share on other sites
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.

##### Share on other sites
[!--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) [snapback]358639[/snapback][/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 #### Share this post ##### Link to post ##### Share on other sites 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().

##### Share on other sites
"I don't know where New York is, how do I get there?"

"It's in America"

"I know that, but where in America?"

"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

##### Share on other sites
[!--quoteo(post=358662:date=Mar 26 2006, 01:42 PM:name=MrLeN)--][div class=\'quotetop\']QUOTE(MrLeN @ Mar 26 2006, 01:42 PM) [snapback]358662[/snapback][/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?"

"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?

##### Share on other sites
Of course I have. Why would I say it doesn't work unless I had tried it? I've tried it multiple times, in different variations, in different orders, with and without quotes or single quotes. What sort of question is that?

It is obvious you're just playing around. If you knew how to fix it you'd just tell me and stop beating around the bush.

If you're going to reply, you should at least give tangable and coherent answers. The point of my post above is that you're telling me you've told me how to fix it and you get some sort of kick out of telling me that you've told me except I've informed you multiple times that it doesn't work.

Look, I should have just not replied.

Either show me the solution or stop wasting both of our time. I don't mind if you don't know the answer. In such a case, just don't reply. I wouldn't get upset with that - of course. But I am not in the mood for cryptic puzzles.

- You either know the solution: Kindly show me, or
- You don't know - That's fine, just stop responding.

MrLeN

##### Share on other sites
Look in these free forums people post suggestions, they may or may not work. It's as simple as that. You are choosing to get upset over it.

In the future, if you need more one-on-one help, I recommend you post for paid/unpaid help in our freelancing topic area.

The suggestions I made were without actually trying your code because from experience I knew it should work.

Anyway, I took your code and actually tried it. The code below works for me. You did not have anything in the form action but I assume you took that out before showing us your code. Also, you used single quotes when writing headinclude to the config file. I commented out your includes since I don't have them. See the comments in the code. Basically, I just added what I already suggested and that was to include/require the config file after the fclose().

I'm having trouble adding the code too. So, I try and PM the code.

##### Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Look in these free forums people post suggestions, they may or may not work. It's as simple as that. You are choosing to get upset over it.
[/quote]

..and I'm fine with that (suggestions). I'm not retarded.

However, I get upset when somene repeatedly tells me that I have been given the answer, and being asked if I've "even bothered to try" and other unhelpful comments, when I have already infomed you that your "suggestion" does not work.

Suggestions: I can handle - and appreciate. Know all statements I can't - and don't.

I am telling you - forget stripslashes. I have been trying for two hours to make stripslashes work now. Once I lose the slashes, I can't get one back. No matter what I try. There must be another solution.

I don't know what you're doing to the code, and I have no idea why you keep insisting it works and now you've even tried my code, and "apparently" it works.. well if so, kindly show me what you did?

I am upset at the situation. You do not need to keep insisting I go away and come back or that I am getting upset with people. I am just annoyed with your replies. For two pages you have insisted you have given me a working answer, yet you have not. The suggestion that I have been provided with the solution is tainted with sarcasm - because it is contradictory to me informing you that the answer does not work. Asking if I've "even bothered to try" is just an insult.

You haven't shown me a single thing. Just two pages of telling me you gave me the answer, and I should calm down and asking me not to be arrogant and telling me you tried my code and it works for you

..well in that case SHOW me. Otherwise, I'll continue to view you as an annoying know all.

Any wonder I get upset with people - sheesh.

MrLeN

##### Share on other sites
This forum is currently not letting me add the simplest of code snippet. If you want, I can email you the code that works for me.

mrlen@mrlen.com

MrLeN

×

• Chat

## 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.