Jump to content

ereg_replace question


vexx

Recommended Posts

I have the following lines of code:

 

$title =ereg_replace("[^[:alnum:]]", " ", $_POST['title']);

$title = ereg_replace(" +",' ',$title);

 

Any inputed title will be shown, except for the special chars. My question is, how can I allow the following chars: ' and -

 

Tnx in advance.

 

p.s. i'm a newbie programmer

Link to comment
Share on other sites

on - is working, but when i try to add ' it gives me the following error "supplied argument is not a valid MySQL result resource". So, the ' char is not working, maybe i did something wrong?

 

I edited the lines like this:

 

$title =ereg_replace("[^[:alnum:]'-]", " ", $_POST['title']);
$title = ereg_replace(" +",' ',$title);

Link to comment
Share on other sites

on - is working, but when i try to add ' it gives me the following error "supplied argument is not a valid MySQL result resource". So, the ' char is not working, maybe i did something wrong?

 

I edited the lines like this:

 

$title =ereg_replace("[^[:alnum:]'-]", " ", $_POST['title']);
$title = ereg_replace(" +",' ',$title);

 

ok,i resolved the ' problem by editiing this line:

$title = ereg_replace(" +'",' ',$title);

 

the problem is now, that ' doesn't appear tho...only -

Link to comment
Share on other sites

somebody advised me to use pre_replace:

 

$title = preg_replace('/[^\w\' -]/', ' ', $_POST['title'] );
$title = preg_replace('/ +/', ' ', $title);

 

The problem is, while - is showing with no problem, the ' is not.

 

example of titles used: test'ing, test-test

 

test-test shows very good, while test'ing isn't even displayed

Link to comment
Share on other sites

Hmm I tryed you preg_replace in my regex test tool and for me the test'ing worked fine...

 

But as effigy said. Since you are accepting ' then you need to do mysql_real_escape_string before you post to database becouse the ' will break the SQL.

Link to comment
Share on other sites

ok,i figured it out, it wasn't escaped, so it works:

 

$title = preg_replace('/[^\w\'-]/', ' ', $_POST['title']);

$title = preg_replace('/ +/', ' ', $title);

$title = mysql_real_escape_string($title); 

 

Now, the only problem is, when i type any title with ' in it, it ads a space before for no reason like this: Test 'ing instead of Test'ing

 

why?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.