Jump to content

Recommended Posts

Folks,

 

 

From this url String, i want to extract the last part of string which is, "fitness spinning".

 

This URL is dynamic an can have any value in that last bit, so how to extract anything btween Two Forward slashes just before .html?

 

Note: It can not be extracted with GET as its not how its designed.

 

Thanks

Natasha

 

Link to comment
https://forums.phpfreaks.com/topic/231849-how-to-extract-certain-bit-from-string/
Share on other sites

try this

<?php
$url="http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html";
preg_match_all("/\/\w{2,20}-\w{2,20}.html/", $url, $matches);
print"<pre>";
print_r($matches);
print"</pre>";
$last = $matches[0][0];
$take_out = array('/', '-', '.html');
$put_in = array('', ' ', '');
$last = str_replace($take_out, $put_in, $last);
print"$last";
?>

 

it outputs this

Array
(
    [0] => Array
        (
            [0] => /fitness-spinning.html
        )

)
fitness spinning

Hey nat.

 

<?php
function getLastPath($url) {
        $trim_path = trim($url, '/');
        $parts = explode('/', $trim_path);
        $last = end($parts);

        return $last;
   }

$insert_url = "http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html";

    echo getLastPath($_SERVER['REQUEST_URI']);//get current page
    echo '<br />';
    echo getLastPath($insert_url);//get inserted url
?>

 

Demo:

http://get.blogdns.com/dynaindex/find-last-path.php

 

try this

<?php
$url="http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html";
preg_match_all("/\/\w{2,20}-\w{2,20}.html/", $url, $matches);
print"<pre>";
print_r($matches);
print"</pre>";
$last = $matches[0][0];
$take_out = array('/', '-', '.html');
$put_in = array('', ' ', '');
$last = str_replace($take_out, $put_in, $last);
print"$last";
?>

 

it outputs this

Array
(
    [0] => Array
        (
            [0] => /fitness-spinning.html
        )

)
fitness spinning

 

 

Thansk but seems this code is not working for the below URL:

 

Here's how to get the position before too.

 

<?php
function getSecondLastPath($url) {
        $path = parse_url($url, PHP_URL_PATH);
        $trim_path = trim($path, '/');
        $positions = explode('/', $trim_path);

        if (substr($path, -1) !== '/') {
            array_pop($positions);
        }
        return end($positions);
   }

$insert_url = "http://natty.com/p/bh-fitness-class-indoor-magnetic-exercise-bike-2-years-parts-/detail/b004r2wuak/fitness-spinning.html";

    echo getSecondLastPath($_SERVER['REQUEST_URI']);//get current page
    echo '<br />';
    echo getSecondLastPath($insert_url);
    echo '<br />';
?>

Demo:

http://get.blogdns.com/dynaindex/find-second-last-path.php

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.