Solved

PHP Explode

Posted on 2014-02-08
2
226 Views
Last Modified: 2014-02-08
I'm a little confused:

I'm trying to extract one part of a url, but am not succeeding.

URL: http://www.MYSITE.com/members/member-name/profile/

	$str = $_SERVER["REQUEST_URI"];
	$array = explode("/", $str, 3);
	$slug_a = $array[2];
	$slug = str_replace('/', '', $slug_a); 
	echo $slug;

Open in new window


Right now $slug is echoing "member-nameprofile"
I need it to only echo "member-name"

Thanks in advance.
0
Comment
Question by:rgranlund
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Please see http://www.laprbass.com/RAY_temp_rgranlund.php

<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28360063.html

$url = 'http://www.MYSITE.com/members/member-name/profile/';

// BREAK ON DIRECTORY SEPARATOR
$arr = explode('/', $url);

// SHOW THE RESULTING ARRAY
echo '<pre>';
print_r($arr);

// SHOW THE REQUESTED DATA ELEMENT
var_dump($arr[4]);

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
The 3 in your explode is limiting the array to only 3 elements, anything over that is just added to the last element of the array since the URI will begin with a slash you have a blank starting element in your array, change it to 4 or just do it the way Ray has.

edit.
Actually limiting it makes no sense in this case! Ray's way is the best way.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 26
Page showing diff display 4 19
How to place a condition in a filter criteria in t-sql? 12 31
PHP string issue 5 16
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

743 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

12 Experts available now in Live!

Get 1:1 Help Now