PHP get part of string

Hi I have a string that I would like to extract the user name from. The string looks like the following:

http://gdata.youtube.com/feeds/api/users/kidrauhl

The string will always have a different user name attached to it like:
http://gdata.youtube.com/feeds/api/users/testUser

How can I always be sure to grab the user name?


thanks!
SolutionabcAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Output is:

kidrauhl
<?php // RAY_temp_solutionABC.php
error_reporting(E_ALL);
$string = 'http://gdata.youtube.com/feeds/api/users/kidrauhl';
echo end(explode('/', $string));

Open in new window

0
 
Ray PaseurCommented:
If the user name is always at the end, you can use explode('/', $string) and take the end() of the resulting array.
0
 
tsmgeekCommented:
$str = "http://gdata.youtube.com/feeds/api/users/kidrauhl";

$result = preg_match("/(http\:\/\/gdata\.youtube\.com\/feeds\/api\/users\/)(.*)$/",$str,$matches);

print_r($matches);

#or

$result = preg_match("/.*\/(.*)$/",$str,$matches);

print_r($matches);

Open in new window

0
 
Ray PaseurCommented:
Sorry - I meant to post a link to the code snippet I posted above.
http://www.laprbass.com/RAY_temp_solutionABC.php

HTH, ~Ray
0
All Courses

From novice to tech pro — start learning today.