clown[NOR] Posted April 9, 2007 Share Posted April 9, 2007 I'm currently working on a calendar project that includes an add entry... but I want to strip the message for all "bad" chars... like now you can use HTML tags and everything... so... i know how to use the str_replace, but I've been trying to understand the regex, but I just cant figure it out... can anyone please help me? Quote Link to comment Share on other sites More sharing options...
veridicus Posted April 9, 2007 Share Posted April 9, 2007 You can find a reference with some examples relating to HTML at DocForge Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 thanks m8... i'll read trough it see if it makes me any smarter =) Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 i've been trying some combinations, but I just cant figure this out... all i know is that it didn't work..haha.. cuz it allowed this message <strong>this </strong><font size="7">is </font><em>a test </em><table border="12"><tr><td>in the </td></tr><tr><td>validation</td></tr></table>process <br />for <br />new <br />entries this is the ereg code i use: (DONT LAUGH!! ) ereg('^[a-åA-Å0-9]', $message) Quote Link to comment Share on other sites More sharing options...
effigy Posted April 9, 2007 Share Posted April 9, 2007 What's a "bad" character--or, often better, what's a "good" character? Use strip_tags to get rid of HTML. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 what i mean with "bad" character is like < > ' /\ etc.. i want 3 things for my message.. normal letters, lower and uprcase, numbers and the " incase i need to qoute something special..hehe btw...thanks for the link Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 even tho i know that these cahrs is the one i want, I just dont manage to adde them to the regex() [a-åA-Å0-9]\[,._-] can anyone help me getting the regex() to work? Thanks In Advance Regards, Clown Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 nobody want to help me? ??? I've been reading on php.net docforge.com regexlib.com and i just don't get it from just that theoretical part.. i need to see it in action here's an explination about what I want to achive.. Valid chars: abcdefghijklmnopqrstuvwxyzæøå ABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ 0123456789 ,.-" () Illegal chars: (if you know any other chars removed.. please let me know) <>'_/\+* well basicly now i'm asking anyone to please give me if not the whole code, at least a guidline I can use. Please guys Thanks In Advance Quote Link to comment Share on other sites More sharing options...
effigy Posted April 9, 2007 Share Posted April 9, 2007 It has only been a few hours since my last response. This is a public forum: people come and go, and so do their responsibilities. Above all, be patient. How is your data encoded? UTF-8? Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 i have no idea... how do i check it? btw sorry... thought it had been much more than that... been awake for about 24 hours now Quote Link to comment Share on other sites More sharing options...
effigy Posted April 9, 2007 Share Posted April 9, 2007 Check the character set of your database (if you're using one) and the one in your meta tag. Quote Link to comment Share on other sites More sharing options...
fert Posted April 9, 2007 Share Posted April 9, 2007 using regex would just waste CPU power use str_replace or str_ireplace instead. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 9, 2007 Share Posted April 9, 2007 fert: Regular expressions are "intelligent," whereas str_replace is literal. In this case he needs to work from exclusion rather than inclusion. It's easier to allow 100 characters, than to disallow tens of thousands. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 9, 2007 Author Share Posted April 9, 2007 that was a really good point effigy..hehe well.. but the thing is... i know what to allow.. I just dont know how to add them to a regex()... i've been reading my ass off, and I'm still not able to make it work Quote Link to comment Share on other sites More sharing options...
fert Posted April 9, 2007 Share Posted April 9, 2007 using regex (haven't tested it, don't know if it'll work) $str=preg_replace("/[<>'/\\+*]/","",$str); using str_replace $chars=array("<",">","'","/","\\","+","*"); $str=str_replace($chars,"",$str); Quote Link to comment Share on other sites More sharing options...
effigy Posted April 10, 2007 Share Posted April 10, 2007 Clown, you cannot have a list of valid characters and a list of invalid characters if they are not comprehensive; therefore, I'd base the expression off of your valid characters. Here's an example: <meta charset="utf-8"/> <pre> <?php ### Borrowed from http://us3.php.net/manual/en/function.utf8-encode.php#58461 function code2utf($num) { if($num<128)return chr($num); if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128); if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128); if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128); return ''; } $latin_block = array(); foreach (range(32, 591) as $code_point) { $latin_block[] = code2utf($code_point); } $greek_block = array(); foreach (range(880, 1023) as $code_point) { $greek_block[] = code2utf($code_point); } $latin_string = join('', $latin_block); $greek_string = join('', $greek_block); echo $big_string = $latin_string . $greek_string; echo '<br>'; echo preg_replace('/[^-,."()a-z0-9\x{00E6}\x{00F8}\x{00E5}]/iu', '', $big_string); ?> </pre> Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 11, 2007 Author Share Posted April 11, 2007 wow..haha =) ok... gonna have to visite that site you borrowed it from... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.