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 109

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 109

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 109

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 34
send post values 7 26
How do I fix this UPDATE error? 7 24
display balance of stars as greyed out 8 16
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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 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