PHP get part of string

Posted on 2011-03-25
Last Modified: 2012-05-11
Hi I have a string that I would like to extract the user name from. The string looks like the following:

The string will always have a different user name attached to it like:

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

Question by:Solutionabc
  • 3
LVL 108

Expert Comment

by:Ray Paseur
ID: 35220107
If the user name is always at the end, you can use explode('/', $string) and take the end() of the resulting array.

Expert Comment

ID: 35220112
$str = "";

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



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


Open in new window

LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 35220122
Output is:

<?php // RAY_temp_solutionABC.php
$string = '';
echo end(explode('/', $string));

Open in new window

LVL 108

Expert Comment

by:Ray Paseur
ID: 35220128
Sorry - I meant to post a link to the code snippet I posted above.

HTH, ~Ray

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Password forgotten. 3 40
Paypal 502 Error 3 66
Download a website to hdd 2 49
array_values - reorder after unset? 5 9
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now