Solved

PHP Explode

Posted on 2014-02-08
2
229 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 109

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
SQL Syntax 14 35
Help with PHP 13 27
AJAX Wordpress Not Reading Variable 2 31
RAISERROR WITH NOWAIT 2 18
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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 …

830 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