Link to home
Start Free TrialLog in
Avatar of Solutionabc
Solutionabc

asked on

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!
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

If the user name is always at the end, you can use explode('/', $string) and take the end() of the resulting array.
$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

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry - I meant to post a link to the code snippet I posted above.
http://www.laprbass.com/RAY_temp_solutionABC.php

HTH, ~Ray