PHP - If URL contains string
You can use a startswith function
(see this stackoverflow post: startsWith() and endsWith() functions in PHP). Here, you test would be:
if (startsWith ($_SERVER['REQUEST_URI'], '/forum/'))
You can also use a regexp: http://php.net/manual/en/regex.examples.php. Here, your test would be:
if (preg_match ('#^/forum/#', $_SERVER['REQUEST_URI']))
Hope this helps.
Check if URL has certain string and match position with PHP
Go like this:
$url = 'www.domain.com/austin/georgetown'; //output: georgetown
if (strpos($url,'georgetown') !== false && strpos($url,'austin') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'georgetown') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'austin') !== false)
{
echo 'austin';
}
first if condition is checking if austin and georgetown, both are there in the url, if yes georgetown will be printed. rest two conditions are just checking for austin and georgetown individually.
See hear Php fiddle -> https://eval.in/715874
Hope this helps.
Check if text contains URL
You can use stristr()
$has_link = stristr($string, 'http://') ?: stristr($string, 'https://');
http://php.net/manual/en/function.stristr.php
Or preg_match()
preg_match('/(http|ftp|mailto)/', $string, $matches);
var_dump($matches);
https://www.php.net/manual/en/function.preg-match.php
a code that checks the current url, checks if it contains a certain string
With javascript you can try as below:window.location.pathname
will give the string added to the main url
<script type="text/javascript">
if ((window.location.pathname === '/orders') {
console.log("orders url");
}
</script>
If php you can try this:
$URL = $_SERVER['PHP_SELF']);
if (str_contains($URL, 'orders')) {
print_r("URL contains orders");
}
PHP to check if a URL contains a query string
For any URL as a string:
if (parse_url($url, PHP_URL_QUERY))
http://php.net/parse_url
If it's for the URL of the current request, simply:
if ($_GET)
How to check current URL inside @if statement in Laravel 5.6
I would tackle this the following way.
@if(str_contains(url()->current(), '/dashboard/attendance/report'))
<a href="#" class="btn btn-danger" target="_blank">Button</a>
@endif
The str_contains()
function checks whether the string (parameter #1) contains the second string. I prefer this function because the Laravel url()
function returns a full url, whereas you would like to check whether the url contains a certain path.
Documentation for the url()->current()
can be found here:
https://laravel.com/docs/5.6/urls#accessing-the-current-url
Edit:
A way to ensure that /dashboard/attendance/report
is at the end of the current url, you could use the following:
@if(substr(url()->current(), -1) == '/dashboard/attendance/report'))
<a href="#" class="btn btn-danger" target="_blank">Button</a>
@endif
Related Topics
PHP Session for Tracking Unique Page Views
Cast the Current Object ($This) to a Descendent Class
Return Multiple Response Data in One Response
PHP Integer and Float Comparison Mismatch
How to Downgrade or Install a Specific Version of Composer
What Literal Characters Should Be Escaped in a Regex
Make a Path Work Both on Linux and Windows
MySQL Get a Random Value Between Two Values
What Causes an Http 405 "Invalid Method (Http Verb)" Error When Posting a Form to PHP on Iis
How to Set 777 Permission on a Particular Folder
Format Text in Excel File via PHP
Composer Require Local Package
Detect Exif Orientation and Rotate Image Using Imagemagick