Jump to content

Recommended Posts

Hi,

 

I am planning on learning Regular Expressions but just don't have the time right now.

 

I am trying to fix something for a friend and just want a quick fix.

 

Please don't reposed with "have you searched Google?" or anything like that as I have and can't find the answer (within 20 mins anyway).

 

I simply want the Regular Expression for checking for the existence of one single character.

 

(1) Check if an @ sign exists in a string.

 

(2) Check if any number exists in a string.

 

...both are for separate checks, so one check is to simply check if an @ sign exists in a string, and the other check is the number one.

 

Please help. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/252373-simple-php-and-regex/
Share on other sites

Ok, great, thanks, although I think strpos would be better.

 

Also, what about the number question, so:

 

(1) "hhdhjdjdhjh1" = yes

(2) "ddddd" = no

(3) "jjjj333" = yes

 

Check if any number appears anywhere in the string.

 

etc.

 

Thanks again for responding.

Hi mate

 

Ok this is testing my limited regex knowledge to the limits here but i think this works:

 

<?php


$pattern2 = '/^(?=.*\d)|(?=.*\d)(?=.*[a-zA-Z]).{1,}$/';

$email  = 'uhuuyyv576576576';

if (preg_match($pattern2, $email)) {

echo "your pattern matched";

}


else {

echo "Your patten didn;t match";

}

?>

 

It should match any pattern of letters and number, or just number but it won't match just letters...hehe confusing myself now...

Hi mate

 

Err I was having a fiddle with this and a much more simpler version would be:

 

<?php 

$pattern5 = '/[\w]?[\d]+/';

$email  = '111eeee';

if (preg_match($pattern5, $email)) {

echo "your pattern matched";


}


else {

echo "Your patten didn't match";

}

?>

 

That is a much simpler way of matching alphanumeric characters or just numeric. It won't match just letters though. Sorry looked at regex sooo long ago that i thought it needed to be a lot more complicated than it did!

Great, thanks, I thought it looked long just to do a simple check, but it worked so I was happy with that!

 

Thanks again.

 

Also, have you any idea how to remove everything from a string except letters, numbers and the @ sign,

 

so:

 

"hello123-123" = "hello123123"

"John o'Leary" = "John oLeary"

"aaaaaa11111111_1111@" = "aaaaaa111111111111@"

 

Thanks again.

Hmm

 

I would try something like

 




$string = 'John_Smith fee334343';

// This is the pattern the pre_replace will use looking for matches
$patterns = array();
$patterns[0] = '/[ \-_]/';


// The array value here that corresponds to the above pattern will replace matches 
//with the contents of the array value. So in this case it replaces anything found
//in the pattern with '' - i.e. removes spaces, hyphens etc.
$replacements = array();
$replacements[0] = '';

echo preg_replace($patterns, $replacements, $string);


 

 

 

Great, thanks, I thought it looked long just to do a simple check, but it worked so I was happy with that!

 

Thanks again.

 

Also, have you any idea how to remove everything from a string except letters, numbers and the @ sign,

 

so:

 

"hello123-123" = "hello123123"

"John o'Leary" = "John oLeary"

"aaaaaa11111111_1111@" = "aaaaaa111111111111@"

 

Thanks again.

Better version

 




$string = 'John_Smith   533r3r3 >>>';

$patterns = array();
$patterns[0] = '/[^\w]/';


$replacements = array();
$replacements[0] = '';

echo preg_replace($patterns, $replacements, $string);




 

This replaces everything except letters, numbers or underscores.

Hi mate

 

Try this as the pattern:

 


$patterns[0] = '/[^\w@.]/';

 

essentially that pattern means replace everything except what is inside the square brackets.

 

The \w applies too all letters, numbers and the underscore. If you also want to add in a hyphen as an allowed character then use:

 

$patterns[0] = '/[^\w@.\-]/';

 

Hope that helps matie :) I have had a good refresher in simple regex today hehe

 

 

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.