Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Checking for a string within a string

how to perform this?

11:43 am on Jul 6, 2007 (gmt 0)

5+ Year Member

Hello forums!
Consider the following case:
$ref_url = "http://www.example.com";
// or $ref_url = "http://example.com" (without 'www')

$check_url = "http://example.com/index.php?action=a&mode=b&blabla";
I want to check the if the $check_url contains the $ref_url or not.
Thanks in advance to all of you.

[edited by: eelixduppy at 11:46 am (utc) on July 6, 2007]
[edit reason] example.com [/edit]

12:38 pm on Jul 6, 2007 (gmt 0)

5+ Year Member

Try this

$ref_url = [url=http://www.php.net/manual/en/function.parse-url.php]parse_url[/url]("www.example.com");
$check_url = parse_url("www.example.com/page.php&x=whatever");

if ($ref_url["host"] == $check_url["host"]) {
echo "URL Found";

[edited by: Gian04 at 1:05 pm (utc) on July 6, 2007]

4:16 am on Jul 9, 2007 (gmt 0)

10+ Year Member


$ref_url = "http://www.example.com";
$check_url = "http://example.com/index.php?action=a&mode=b&blabla";

if (strstr($ref_url, $check_url))
echo 'Eureka';


Try this, I didn't test it, but it seems the strstr() function should do it.

6:47 am on Jul 9, 2007 (gmt 0)

10+ Year Member

Actually, I think this might work better than what I posted before...


$ref_url = 'http://example.com';
$check_url = 'http://example.com/index.php?action=a&mode=b&blabl';

if (strstr($check_url, $ref_url))

{echo 'Eureka!';}
else {echo ' Not found! ';}


From PHP manual:
Note: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead.
I could have done this, but just wanted to fix what I wrote first out of my head.

I actually like Gian's solution better than mine too, btw, but I thought I should show another way to skin this cat.