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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 110

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 110

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

626 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