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
https://forums.phpfreaks.com/topic/103406-ereg_replace-question/
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);

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 -

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

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?

Archived

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

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