str_replace query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashleek007
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 5:50 am

str_replace query

Post by ashleek007 »

Hey,

I've got a url like this....

dpages/38/test1.php

I want to remove everything from this string so i am left with just: -

38

I know I need to use the str_replace function, but i cant seem to get it working properly...

Is there a simple one liner that can do this... to strip all before, after and including the '/'...

Cheers,
Ash :roll:
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

Code: Select all

<?php
$string = 'dpages/38/test1.php';
$start = strpos($string, '/') + 1;
$len = strrpos($string, '/') - $start;
echo substr($string, $start, $len);
?>
ashleek007
Forum Newbie
Posts: 9
Joined: Fri Aug 12, 2005 5:50 am

Post by ashleek007 »

Code: Select all

$url_id = $row['url'];
$url_id = explode('/', $url_id, 3);
echo $url_id[1];
thats what ive found works.... the explode() function...

Ash
Post Reply