views:

79

answers:

5

I want to use php to search the current url for com_agora and if it finds it to display something and if it doesn't to display something else

the problem is there can be lots of characters after com_agora in the url

an example would be this com_agora&task=cat_view&gid=41&Itemid=

so how would I tell it find it while not caring what characters come after a?

+4  A: 
$uri = $_SERVER['REQUEST_URI']; // or taken from another source //
if( strpos($uri, 'com_agora') !== false ){
   // Your action goes here! //
}

If you need more complex parameters operations please use parse_str.

Alin Purcaru
+2  A: 
if(strpos($url, 'com_agora') !== FALSE) { /* do something */ }
knittl
This will also return true if `com_agora` is a part of another variable or value
Pekka
@pekka: the op only asked if com_agora is in the string, he does not care about anything else in the string. at least that's how i read the question
knittl
@knittl I think this is a case of the OP not asking the right question :) At least it looks that way from the example he gives
Pekka
@pekka: probably, but as it stands now it's a valid answer ;)
knittl
+3  A: 

I am assuming you are talking about the query string here (the part after the ?)

For a bullet-proof approach, use parse_str() to take apart the query string and to see whether there is a parameter named com_agora. This has the advantage that it will ignore the occurrence of the search term anywhere else in the string (e.g. in another parameter value).

$query_string = $_SERVER["QUERY_STRING"]; // e.g. com_agora&task=cat_view
$query_string_parsed = array();       

parse_str($query_string, $query_string_parsed); 

// Search for "com_agora"
$found = array_key_exists("com_agora", $query_string_parsed); 
Pekka
+1  A: 

Joomla has a class that allows you to work with URL JURI, you can learn more about JURI class on Joomla's documentation site.

Here is fully functional code, that will do what you what it to do

<?php

// Sample URL
$url = "http://www.mysite.com/index.php?option=com_agora&amp;task=cat_view&amp;gid=41&amp;Itemid=5";

// Using JFactory::getURI() without parameter will give you URI of current webpage
$uri = JFactory::getURI($url);

// Here is the structure of the object
//
//object(JURI)[136]
//  public '_uri' => string 'http://www.mysite.com/index.php?option=com_agora&amp;task=cat_view&amp;gid=41&amp;Itemid=5' (length=78)
//  public '_scheme' => string 'http' (length=4)
//  public '_host' => string 'www.mysite.com' (length=14)
//  public '_port' => null
//  public '_user' => null
//  public '_pass' => null
//  public '_path' => string '/index.php' (length=10)
//  public '_query' => string 'option=com_agora&task=cat_view&gid=41&Itemid=5' (length=46)
//  public '_fragment' => null
//  public '_vars' => 
//    array
//      'option' => string 'com_agora' (length=9)
//      'task' => string 'cat_view' (length=8)
//      'gid' => string '41' (length=2)
//      'Itemid' => string '5' (length=1)

// Get and output option parameter from the URI
echo 'Option = ' . $uri->getVar('option');
//  output = com_agora

?>
Alex
+1  A: 

As you are in Joomla simply do this:

$option = JRequest::getVar('option','','GET');

The $option variable will then hold the value of whatever is in the URL and you can then go:

if ($option == 'com_agora') {
  //Do something
}
Soren Beck Jensen