Jump to content

how to extract certain bit from string?


natasha_thomas

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.