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
}
worked for me with php
if(strpos($_SERVER['REQUEST_URI'], 'shop.php') !== false){
echo 'url contains shop';
}
This worked for me:
// Check if URL contains the word "car" or "CAR"
if (stripos($_SERVER['REQUEST_URI'], 'car' )!==false){
echo "Car here";
} else {
echo "No car here";
}
If you want to use HTML in the echo, be sure to use ' ' instead of " ".
I use this code to show an alert on my webpage https://geaskb.nl/
where the URL contains the word "Omnik"
but hide the alert on pages that do not contain the word "Omnik" in the URL.
Explanation stripos : https://www.php.net/manual/en/function.stripos
strstr didn't exist back then?
if(strstr($_SERVER['REQUEST_URI'], "car")) {
echo "car found";
}
This must be one of the easiest methods right?
Have a look at the strpos function:
if(false !== strpos($url,'car')) {
echo 'Car exists!';
}
else {
echo 'No cars.';
}
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...
Starting with PHP 8 (2020-11-24), you can use str_contains:
if (str_contains('www.domain.com/car/', 'car')) {
echo 'car is exist';
} else {
echo 'no cars';
}
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..
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (!strpos($url,'car')) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
This seems to work.
Related
I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
However if the url contains the word "care" or "car2" it will also trigger "Car Exists". How do I get a match if only "car" is found?
Change
if (strpos($url,'car') !== false) {
into
if (preg_match('/\bcar\b', $url) !== 0) {
Basically, you search for word car with no other alphanumeric surrounding it.
Hope this helps!
I have this code:
<?php $url = JURI::getInstance()->toString();
if ($url == "http://example.com/news/latest/"){
echo "This is latest page";
} else {
echo "This is not latest page";
}
?>
What I'm trying to do is instead of 'http://example.com/news/latest/', how can I select the pages/items under /latest/. If it makes any more sense, here's a syntax:
if ($url == "http://example.com/news/latest/" + ANYTHING UNDER THIS)
I cannot use not equal to ($url !=) since it will include other parent pages not equal to /latest/. I just want what's under it. If anyone understands it, I need help on how to put it into code.
Update:
What I'm trying to do is if the page is example.com/news/latest, it will echo "Latest". And if for example, I am in example.com/news/latest/subpage1/subpage2, it will echo "You are in a page that is under Latest." Anything beyond "Latest" will echo that.
$str = 'example.com/news/latest/dfg';
preg_match('/example.com\/news\/([^\/]+)\/?(.*)/', $str, $page);
if(isset($page[2]) && $page[2])
echo 'You are under: ' , $page[1];
elseif(isset($page[1]))
echo 'At: ' , $page[1];
else
echo 'Error';
Edit: after clarification switched to regular expression.
Use a regular expression:
$matches = array();
if((preg_match('#http://example\.com/news/latest/(.*)#', $url, $matches)) === 1) {
if(strlen($matches[0]) > 0) {
echo "You're at page: $matches[0]";
} else {
echo "You're at the root";
}
} else {
// Error, incorrect URL (should not happen)
}
EDIT: Fixed, untested so you may have to tweak it a little
Now I want to check if this text box contains one word or two, for example
if ($_POST['mytext'] == two words){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and I tried
if ($_POST['mytext'] > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
and it didn't work
that what I mean so how to make it?
Hope to find a way to do that.
Thanks
If you define two words as "some characters followed by one space followed by some characters" then you can do something like:
$mytext = $_POST["mytext"];
$parts = explode(" ", $mytext);
if (count($parts) !== 2) {
throw new Exception("too many or too little!");
}
if (strlen($parts[0]) === 0 || strlen($parts[1]) === 0) {
throw new Exception("not enough characters!");
}
Keep in mind that this allows a string like "# !"
Use str_word_count():
if (str_word_count($_POST['mytext']) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
you could use the
`substr_count('some text', ' ');
it will return the number of space,.
try this
$text= preg_split(" ",$_POST['mytext']);
if (count($text) > 1){
echo "That is Perfect";
} else{
echo "We don't accept this";
}
I have the following three possible urls..
www.mydomain.com/445/loggedin/?status=empty
www.mydomain.com/445/loggedin/?status=complete
www.mydomain.com/445/loggedin/
The www.mydomain.com/445 part is dynamically generated and is different each time so I can't do an exact match, how can i detect the following...
If $url contains loggedin but DOES NOT CONTAIN either /?status=empty OR /?status=complete
Everything I try fails as no matter what it will always detect the logged in part..
if(strpos($referrer, '?status=empty')) {
echo 'The status is empty';
}
elseif(strpos($referrer, '?status=complete')) {
echo 'The status is complete';
}
elseif(strpos($referrer, '/loggedin/')) {
echo 'The status is loggedin';
}
Slice up the URL into segments
$path = explode('/',$referrer);
$path = array_slice($path,1);
Then just use your logic on that array, the first URL you included would return this:
Array ( [0] => 445 [1] => loggedin [2] => ?status=empty )
You could do something like this:
$referrer = 'www.mydomain.com/445/loggedin/?status=empty';
// turn the referrer into an array, delimited by the /
$url = explode('/', $referrer);
// the statuses we check against as an array
$statuses = array('?status=complete', '?status=empty');
// If "loggedin" is found in the url, and count the array_intersect matches, if the matches = 0, none of the statuses you specified where found
if( in_array('loggedin', $url) && count(array_intersect($url, $statuses)) == 0 )
{
echo 'The user is logged in';
}
// if the complete status exists in the url
else if( in_array('?status=complete', $url) )
{
echo 'The status is complete';
}
// if the empty status exists in the url
else if( in_array('?status=empty', $url) )
{
echo 'The status is empty';
}
I would recommend looking at array_intersect, it is quite useful.
Hope it helps, not sure if this is the best way of doing it, but might spark your imagination.
Strpos is probably not what you want to use for this. You could do it with stristr:
if($test_str = stristr($referrer, '/loggedin/'))
{
if(stristr($test_str, '?status=empty'))
{
echo 'empty';
}
elseif (stristr($test_str, '?status=complete'))
{
echo 'complete';
} else {
echo 'logged in';
}
}
But it's probably easier/better to do it with regular expressions:
if(preg_match('/\/loggedin\/(\?status=(.+))?$/', $referrer, $match))
{
if(count($match)==2) echo "The status is ".$match[2];
else echo "The status is logged in";
}
PHP:
function is_homepage()
{
}
if(is_homepage())
{
echo 'You are on the homepage';
}
else
{
echo 'You are not on the homepage';
}
Explanation:
is_homepage, should work in all these cases:
http://www.domain.com
https://www.domain.com
http://domain.com
http://domain.com/?param=value
http://domain.com/index.php?param=value
Where it shouldn't work:
http://subdomain.domain.com
http://domain.com/otherfile.php?param=value
etc.
do a
print_r($_SERVER);
and you'll see all the data which will help you achieve this.
I would use
$_SERVER['PHP_SELF']
to identify the file\page I'm currently working with.
It depends of course on how your PHP script is laid out. Though the following solution would work in most cases:
$_SERVER['SCRIPT_NAME'] == '/index.php'
function is_homepage()
{
return ( ( $_SERVER['HTTP_HOST'] == 'www.domain.com' || $_SERVER['HTTP_HOST'] == 'domain.com') && substr( $_SERVER['REQUEST_URI'], 0, 9 ) == 'index.php' );
}
if(is_homepage())
{
echo 'You are on the homepage';
}
else
{
echo 'You are not on the homepage';
}