Segment in URL

Submitted by pabloleone - 6 years 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;
    }
}