Solved

PHP get part of string

Posted on 2011-03-25
4
254 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
  • 3
4 Comments
 
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.
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 109

Accepted Solution

by:
Ray Paseur earned 500 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 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

820 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