Solved

PHP Explode

Posted on 2014-02-08
2
230 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
[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
2 Comments
 
LVL 110

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

710 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