Segment in URL

Submitted by pabloleone - 1 month ago

Check if string is in any url segment or at the one specified.

<?php

if (!function_exists('inUrl')) {
    /**
     * Check if string is in any url segment or at the one specified.
     *
     * @param  string  $value   Segment value/content.
     * @param  integer $segment Segment position.
     * @return bool
     */
    function inUrl($value, $segment = -1)
    {
        if ($segment !== -1 && Request::segment($segment) === $value) {
            return true;
        }

        foreach (Request::segments() as $segment) {
            if ($segment === $value) {
                return true;
            }
        }

        return false;
    }
}

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks