homepage Welcome to WebmasterWorld Guest from 54.204.64.152
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
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?
PHPycho




msg:3387637
 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]

 

Gian04




msg:3387665
 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




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

<?php

$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




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

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

<?php

$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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved