0

I know i can get all segments from url like this

Lets say i have this example link

www.example.com/de/products.html

Using url_helper like this:

$data['url'] = $this->uri->uri_string();

I will get value like this

de/products

But i dont need first segment de, only products, the problem is that i dont know how many segments it will be, i only need to remove the first

Is there possible to forget first segment with url helper in CI?

3 Answers 3

1

Try like this...

Use the php's explode() function to make the url string as array.Then apply array's array_shift() function which always removes the first element from array.

Code is looks like as below

        $data= $this->uri->uri_string();
        $arr=explode('/', $data);
        array_shift($arr);
        //print_r($arr);

Then use the php's implode() method to get the URI without first segment.Hope it will works...

$uri=implode('/',$arr);
echo $uri;
Sign up to request clarification or add additional context in comments.

2 Comments

@Miomir Dancevic thanks.....may be simple but tricky....lets enjoy with codeigniter.
@HikmatSijapati Already inbuilt with $this->uri->segment_array();. Docs.
1

example:

<?php  
      $data=$this->uri->segment(2);
      $val=explode('.', $data);
      echo $val[0]; 
?>

Comments

0

There is no URL helper in the CI to forget the first segment. However you can easily make a custom one and put @Hikmat's answer below it in the application/helpers/MY_url_helper.php in the Core folder.

e.g.

function my_forget_first_segment() {
    $data= $this->uri->uri_string();
        $arr=explode('/', $data);
        array_shift($arr);
        $uri=implode('/',$arr);
        return $uri;
}

Before Edit answer.

You need to try this 

$second_segment = $this->uri->segment(2);

From Codeigniter documentation -

$this->uri->segment(n);

Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

http://example.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this:

1. news
2. local
3. metro
4. crime_is_up

The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:

$product_id = $this->uri->segment(3, 0);

1 Comment

@Miomir Dancevic i think your problem is sloved on my answer.See first if not comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.