Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP get part of string

Posted on 2011-03-25
4
Medium Priority
?
271 Views
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:

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!
0
Comment
Question by:Solutionabc
[X]
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
4 Comments
 
LVL 111

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.
0
 
LVL 5

Expert Comment

by:tsmgeek
ID: 35220112
$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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35220122
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35220128
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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