I would like to know if some word is present in the URL.
For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo 'car is exist' and if there's nothing it would echo 'no cars'.
I would like to know if some word is present in the URL.
For example, if word car is in the URL, like www.domain.com/car/ or www.domain.com/car/audi/ it would echo 'car is exist' and if there's nothing it would echo 'no cars'.
Try something like this. The first row builds your URL and the rest check if it contains the word "car".
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
I think the easiest way is:
if (strpos($_SERVER['REQUEST_URI'], "car") !== false){
// car found
}
$url = " www.domain.com/car/audi/";
if (strpos($url, "car")!==false){
echo "Car here";
}
else {
echo "No car here :(";
}
See strpos
manual
if( strpos( $url, $word ) !== false ) {
// Do something
}
strstr didn't exist back then?
if(strstr($_SERVER['REQUEST_URI'], "car")) {
echo "car found";
}
This must be one of the easiest methods right?
worked for me with php
if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
Have a look at the strpos function:
if(false !== strpos($url,'car')) {
echo 'Car exists!';
}
else {
echo 'No cars.';
}
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'car')) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
This seems to work.
Surely this is the correct way round....
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'mysql')) {
echo 'No mysql.'; //swapped with other echo statement
} else {
echo 'Mysql exists.';
}
Otherwise its reporting the opposite way it should...
You can try an .htaccess method similar to the concept of how wordpress works.
Reference: http://monkeytooth.net/2010/12/htaccess-php-how-to-wordpress-slugs/
But I'm not sure if thats what your looking for exactly per say..
The URL parameters and received from a global variable called $_GET
which is in fact an array. So, to know if a URL contains a parameter, you can use the isset()
function.
if (isset($_GET['yourparametername'])) {
//The parameter you need is present
}
Afterwards, you can create separate array of such parameter you need to attach to a URL.
For example:
if(isset($_GET['param1'])) {
\\The parameter you need is present
$attachList['param1'] = $_GET['param1'];
}
if(isset($_GET['param2'])) {
$attachList['param2'] = $_GET['param2];
}
Now, to know whether or not, you need a ?
symbol, just count this array
if(count($attachList)) {
$link .= "?";
// and so on
}
Update:
To know if any parameter is set, just count the $_GET
if(count($_GET)) {
//some parameters are set
}
if ( stristr( SITE_HOST, 'your site' ) ) {
$protocol = ( !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
$request = ( !empty( $_SERVER['HTTP_X_ORIGINAL_REQUEST'] ) ) ? $_SERVER['HTTP_X_ORIGINAL_REQUEST'] : $_SERVER['REQUEST_URI'];
echo $protocol .'your site'. $request;
}
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpbrk($url, 'test') ) {
// Do something...
}
else {
// Do another thing
}
(>PHP 5)
you can use this function as this code sir
if((strpos($_SERVER['REQUEST_URI'],'car') !== false))
echo'<h3 class=" edit-top">Car Exist</h3>';
else
echo'<h3 class=" edit-top">Not</h3>';