homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Checking for a string within a string
how to perform this?

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

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)

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]

natural number

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


$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.

natural number

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

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved