...

View Full Version : find a?



jwilson122
11-05-2010, 03:18 PM
Hey, What function / how would I find if a string has something? Say I have a string like:


$string = 'Hello their! This is my post :) btw, my name is Justin.';

Say I wanted to find out if my name "Justin" is inside that string..
so:


whateverfunction('Justin', $string)


So I think you should get my idea now though :P

Thanks a lot!

angst
11-05-2010, 03:25 PM
have a look at preg_match()

http://php.net/manual/en/function.preg-match.php

MRushton
11-05-2010, 03:25 PM
One way of doing it is the following:


if (strpos($string, 'Justin') !== false)
:
return true;
endif;

This is case sensitive (use stripos for case insensitive), but will also return true if the string contains "ffafafdjustinDSd". preg_replace can be used if you want to prevent this latter example from returning true (as mentioned in the previous post).

poyzn
11-05-2010, 03:26 PM
preg_match() (http://ru.php.net/manual/en/function.preg-match.php)

kbluhm
11-05-2010, 03:30 PM
strpos() is the superior option in this case.

There is also stripos() that will perform a case-insensitive match.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum