Jump to content

Simple PHP and Regex


johnsmith153

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.