Skip to content
Advertisement

I want to remove text between fifth and sixth occurrence of “/” [closed]

This is url stored in variable

https://photoland2.etrafficgroup.com.au/frames-and-albums/frames/wooden-frames/106-a3-297-x-42cm-2/product/555-black-wooden-frame-with-gold-stripe-a3-42×29-7cm-size-1-7cm-wide/

I want to remove text between fifth and sixth occurrence of “/” (106-a3-297-x-42cm-2) and one /

Output should look like this:

https://photoland2.etrafficgroup.com.au/frames-and-albums/frames/wooden-frames/product/555-black-wooden-frame-with-gold-stripe-a3-42×29-7cm-size-1-7cm-wide/

How can I achieve this?

Advertisement

Answer

You an do it by many ways,. One way to achieve this way using parse_url(),

  <?php
  $url = 'https://photoland2.etrafficgroup.com.au/frames-and-albums/frames/wooden-frames/106-a3-297-x-42cm-2/product/555-black-wooden-frame-with-gold-stripe-a3-42x29-7cm-size-1-7cm-wide/';
  $initial = parse_url($url);
  $result = array_filter(explode('/',parse_url($url)['path']));
  unset($result[4]);
  $filtered = implode('/',$result);
  echo $initial['scheme'].'://'.$initial['host'].'/'.$filtered.'/';
  ?>

DEMO: https://3v4l.org/60rZB

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement