Current location: Hot Scripts Forums » Programming Languages » PHP » Finding a string within a string


Finding a string within a string

Reply
  #1 (permalink)  
Old 12-28-07, 06:48 PM
leeprice2006 leeprice2006 is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Finding a string within a string

how can i find if a string contains a certain string,

basically what i want to do is:

PHP Code:

<?php


$email 
$_POST['email'];

if(
$email containts "@hotmail.com") {
   echo(
"Can not use an hotmail address");
}
?>
Reply With Quote
  #2 (permalink)  
Old 12-28-07, 07:10 PM
leeprice2006 leeprice2006 is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
ignore this post, i've just give myself a flash course in regular expressions and found the answer
Reply With Quote
  #3 (permalink)  
Old 01-01-08, 12:59 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
No need for regular expressions. Have a look at strpos() and stripos() if all you're looking for is a string.

PHP Code:

$email 'user@hotmail.com';

if ( 
stripos$email'hotmail.com' ) !== FALSE )
{
    echo 
'Cannot use a hotmail address';

...or you could use:
PHP Code:

$email    'user@hotmail.com';
$provider strtolowerendexplode'@'$email ) ) );

if ( 
$provider == 'hotmail.com' )
{
    echo 
'Cannot use a hotmail address';

...in case, for whatever reason, hotmail.com is part of their valid email, ie: hotmail.com@gmail.com.

You could also peek at strstr() and stristr():
PHP Code:

$email    'user@hotmail.com';
$provider strtolowerstrstr'@'$email ) );

if ( 
$provider == '@hotmail.com' )
{
    echo 
'Cannot use a hotmail address';

Searching by string is much faster than searching by regular expression.
__________________
The toxic ZCE

Last edited by Keith; 01-01-08 at 01:22 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP & Database Help!!! Diabolical ASP.NET 11 12-09-07 08:58 AM
Finding commas in a string dodotopia PHP 3 04-02-05 06:33 PM
Finding a number within a string dannyallen PHP 1 08-14-04 10:55 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 07:33 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.