$_GET - how to check if input contains specific string

Hello there,

I have a $_GET input form where someone should input a URL address, and I need to do following:
check if $_GET contains specific link. For example link can be like example.com/category/topic/story.html
I need to check if the link is from “example.com” site, and if so perform something, then check if it contains “exampleexample.com” and then perform something etc.

So from the list of sites that can be added I need to check if the URL comes from few specific ones and use the “if” statement to perform further actions.

Thanks in advance.

There are a few different ways to handle the problem. I am a big fan of Regular Expressions. So,

[php]
$urls = [
exampleexample.com’,
example.com’,
];

$input = ‘example.com/category/topic/story.html’;

foreach ( $urls as $url ){
if ( preg_match("/$url/", $input)){
echo “

Found {$url} in {$input}

”;
} else {
echo “

Could not find {$url} in {$input}

”;
}
}
[/php]

I think preg_match is a bit overkill for searching a specific string. It’s just an opinion of course. You could find it in the following manner. Perhaps we could test it to see.

[php]
$urls = [
exampleexample.com’,
example.com’,
];

$input = ‘example.com/category/topic/story.html’;
$testing = array_search($input,$urls);
if($testing !== false) {
echo $testing . ’ was found!’;
} else {
echo $input . ’ does not exist.’;
}
[/php]

So, I went ahead and tested it. This was the results.

Array
(
    [0] => Array
        (
            [Number_of_Loops_to_Perform_Test] => 10,000
            [Number_of_Tests_Performed] => 2
            [Start_of_Test] => 04-18-2016 05:32:13pm
        )

    [Test: 1] => Array
        (
            [Code:] => 
                                $urls = [
				              'exampleexample.com',
				             'example.com',
				             ];
  
                               $input = 'example.com/category/topic/story.html';
  
                                foreach ( $urls as $url ){
		                  if ( preg_match("/$url/", $input)){
			             $store =  "<p>Found {$url} in {$input}</p>";
		                 } else {
			            $store =  "<p>Could not find {$url} in {$input}</p>";
		                 }
	                   }
            [Start] => 1461015133.914
            [End] => 1461015133.9573
            [Elapsed] => 0.04325
        )

    [Test: 2] => Array
        (
            [Code:] => 
                        $urls = [
                                      'exampleexample.com',
                                      'example.com',
                                      ];
  
                        $input = 'example.com/category/topic/story.html';
                        $testing = array_search($input,$urls);
                        if($testing !== false) {
                            $store =  $testing . ' was found!';
                        } else {
                            $store =  $input . ' does not exist.';
                        }
            [Start] => 1461015133.9576
            [End] => 1461015133.9664
            [Elapsed] => 0.00885
        )

)

Thanks for your answer.

In the mean time, I used:
[php]if( strpos( $_GET[‘url’], $blabla) ){

}[/php]
And i have $blabla = “blabla.com”; so it’s defined and it seems to be working nicely right now.
Any reason not to use this though?

Thanks once again!

Sponsor our Newsletter | Privacy Policy | Terms of Service