Find a string within a string

Soldato
Joined
4 Jul 2004
Posts
2,647
Location
aberdeen
Hello
I want to do the following:
Get the referrer
If the referrer includes "google" display $xxx
else display $yyy

How can i check if the referrer has a certain string (ie "google") in it?
All i've got so far is basically "$ref = getenv('HTTP_REFERER');"

(ps: this is in php)
 
stristr.

Code:
$str = 'foobar';

if ( stristr($str, 'foo') )
    echo 'string contains "foo"';

Or alternatively the slightly faster strpos, although it's case sensitive:

Code:
$str = 'foobar';

if ( strpos($str, 'foo') !== false )
    echo 'string contains "foo"';
 
Last edited:
Cheers :)
Another Q now:

I want to validate that $foo is in the format of "dd/mm/yy @ hh/mm".

How can i do a simple thing in the basic style of:
if (check($foo, "dd/mm/yy @ hh/mm") == true) {
echo "correct method";
}
else {
echo "sorry you must have entered it wrong etc";
}

(of course mm = numbernumber etc)
 
stripos also available from >=php5

or for php4 compat:
Code:
if (!function_exists('stripos'))
{
    function stripos($haystack, $needle, $offset = 0)
    {
        return strpos(strtolower($haystack), $strtolower($needle), $offset);
    }
}
 
Chronicle said:
Cheers :)
Another Q now:

I want to validate that $foo is in the format of "dd/mm/yy @ hh/mm".

How can i do a simple thing in the basic style of:
Code:
if (check($foo, "dd/mm/yy @ hh/mm") == true) {
    echo "correct method";
}
else {
    echo "sorry you must have entered it wrong etc";
}
(of course mm = numbernumber etc)
try:
Code:
if (preg_match('#^\d{2}/\d{2}/\d{2} @ \d{2}/\d{2}$#is', $string))
{
    echo 'Date format ok!';
}
 
Back
Top Bottom