vexx Posted April 29, 2008 Share Posted April 29, 2008 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 More sharing options...
effigy Posted April 29, 2008 Share Posted April 29, 2008 [^[:alnum:]'-] I think Link to comment https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-529553 Share on other sites More sharing options...
vexx Posted April 29, 2008 Author Share Posted April 29, 2008 r u sure?..not working it seems Link to comment https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-529883 Share on other sites More sharing options...
effigy Posted April 29, 2008 Share Posted April 29, 2008 Works for me: <pre> <?php echo ereg_replace("[^[:alnum:]'-]", ' ', "abc123.,:'-"); ?> </pre> abc123 '- Link to comment https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-529892 Share on other sites More sharing options...
vexx Posted April 30, 2008 Author Share Posted April 30, 2008 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 https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-530167 Share on other sites More sharing options...
vexx Posted April 30, 2008 Author Share Posted April 30, 2008 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 https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-530175 Share on other sites More sharing options...
effigy Posted April 30, 2008 Share Posted April 30, 2008 If you're leaving quotes in $title then using it within SQL, you need to use mysql_real_escape_string. Link to comment https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-530317 Share on other sites More sharing options...
vexx Posted April 30, 2008 Author Share Posted April 30, 2008 no, I don't use quotes in title, the only char i need is ' ..but i hope i didn't got your message wrong now Link to comment https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-530439 Share on other sites More sharing options...
vexx Posted May 1, 2008 Author Share Posted May 1, 2008 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 https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-530900 Share on other sites More sharing options...
aCa Posted May 1, 2008 Share Posted May 1, 2008 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 https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-530930 Share on other sites More sharing options...
vexx Posted May 2, 2008 Author Share Posted May 2, 2008 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 https://forums.phpfreaks.com/topic/103406-ereg_replace-question/#findComment-531574 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.