Jump to content

Recommended Posts

Hello

 

I'm currently using CodeIgniter and I want to extend the validation library.

 

I am using the following callback function in my forms:

 

function alpha_numeric($str) {
return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE;
}

 

This checks whether or not the field contains alphanumeric characters and returns TRUE or FALSE.

 

I need to create a new function that will ONLY ALLOW the following characters:

 

@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^}{\[~]|€

 

If the string contains any other character it will return FALSE!

 

I am aware that the metacharacters need to be escaped which leaves me with the following:

 

@£\$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'\(\)\*\+,-\./0123456789:;<=>\?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà\^}{\\\[~]\|€

 

Now, this is where I get stuck! Is anyone able to help me create a new function based on the above "alpha_numeric" function to help me achieve my goal?

 

If this new function returns TRUE I would then like to run it through another check which adds +1 to a variable called $counter for every occurrence of any the following characters:

 

^}{\[~]|€

 

I am so stuck on this it's unbelievable and any help will be GREATLY appreciated! :)

 

Thanks very much in advance.

 

Regards

Chris

 

P.S. I am also after another two functions based on the above "alpha_numeric" function.

 

The first: Strings must be between 3 & 16 characters in length and can only comprise alphanumeric characters or the hyphen (-) character. They cannot begin or end with a hyphen.

 

The second: Strings must be up to 11 characters starting with a letter and consisting of letters, numbers, spaces, full stops and hyphens.

 

I have exactly $10.68 USD in my PayPal account and I'll happily "donate" this to anyone that can help. I know there are probably some kind people out there willing to do this for free but I'll happily make a PayPal payment to the first person to reply. It's my way of buying you a bottle of wine via the web!

 

Thanks

Chris

Link to comment
https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/
Share on other sites

Thank you Daniel - I certainly will.

 

Once the first function returns TRUE I would then like to run another check which adds +1 to a variable called $counter for every occurrence of any the following characters:

 

^}{\[~]|€

 

So say if I had the following string:

 

Hello! My name is [Chris]. My currency is the euro (€).

 

The $counter variable would be: 3

 

Could this be achieve using Regex or would I be better off looking at string functions?

 

Regards

Chris

This is what I came up with but I can't help but think there must be a more effective way?

 

<?php
$string = "Hello! My name is [Chris]. My currency is the euro (€).";

$counter = 0;

$characters = array("^", "}", "{", "\\", "[", "~", "]", "|", "€");

foreach($characters as $character) {
$counter = $counter + substr_count($string, $character);
}

echo $counter;
?>

 

Regards

Chris

  • 3 weeks later...

Hi there

 

I'm having trouble with the following function:

 

if(preg_match('#[^' . preg_quote('@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€', '#') . 'A-Za-z0-9]#i', $message)) {
			echo "Good!";
		}
		else {
			echo "Bad!";
		}

 

Basically, I am taking input from a textarea ($message) which can contain only the following characters:

 

@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€A-Za-z0-9

 

If the text contains a combination of ONLY these characters it echo's "Good!".

 

If it contains ANY OTHER characters such as © it would echo "Bad!"

 

I can't figure out where I am going wrong - is anyone able to help?

 

Much appreciated - Chris

I think the problem is because there are wierd characters in there.

 

I had to use mb_strlen to get the string length of the message.

 

Is there anyway to correct the above to work with these characters.

 

I've seen mb_ereg but not sure how to implement it - is anyone able to shed some light?

 

Cheers

Chris

Uhm... that print_r() shouldn't have been there... should have been:

 

function countContains($string, $chars)
{
preg_match_all('#[' . preg_quote($chars, '#') . ']#', $string, $matches);
return count($matches[0]);
}

 

Sorry.

Hello, it's just a side issue, but you could rewrite it in this way:

 

function countContains($string, $chars){
return preg_match_all('#[' . preg_quote($chars, '#') . ']#', $string, $matches);
}

 

 

You need to turn on the UTF8 modifier in your pattern.

 

if(preg_match('#[^' . preg_quote('@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€', '#') . 'A-Za-z0-9]#iu', $message)) {
			echo "Good!";
		}
		else {
			echo "Bad!";
		}

 

 

Note the "u" at #iu', $message)

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.