Jump to content

Help: regex, ereg or str_replace?


clown[NOR]

Recommended Posts

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?

Link to comment
Share on other sites

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!!  ;D)

ereg('^[a-åA-Å0-9]', $message)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

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.