Solved

PHP Explode

Posted on 2014-02-08
2
227 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
ID: 39844719
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
ID: 39844728
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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 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 …

920 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

13 Experts available now in Live!

Get 1:1 Help Now