Remove Anything After The Forward Slash

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Remove Anything After The Forward Slash

Post by djwk »

Hi,

How would I remove any text after the forward slash in a string..

Lets say I have

$strdata = "google.com/adsense";

How would I remove the "/" and "adsense".

But I want to make sure I can remove absolutley anything after the forward slash (not just adsense)

How would I go about this?
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

Im not sure, but you could use $_SERVER['HTTP_HOST']; to get "www.google.com" straight away.
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post by djwk »

thanks, did the job :)
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

It will only work to get the domain of the page your on though. Im guessing you'll have to use regex to strip down any other domains.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If you're not comfortable with regex you could use parse_url()

There are plenty of pre-written URL/URI regex patterns available through search if you want to go that route.
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post by djwk »

Could anyone help me out with either regex or parse_url I've been looking around and I just can't figure out how to use the functions properly.

An example piece of code would be great.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's an example on the documentation page linked.
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post by djwk »

There may be but as I said, I cant understand it.

I tried the code in the first reply to the function:

Code: Select all

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));

echo parse_url($url, PHP_URL_PATH);
?>

and all I get is:

Array ( [scheme] => http [host] => hostname [user] => username [pass] => password [path] => /path [query] => arg=value [fragment] => anchor )
Warning: parse_url() expects exactly 1 parameter, 2 given in /******/test.php on line 6
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You aren't using PHP 5.1.2 or after, so the second parameter doesn't exist.
ChangeLog

Version Description
5.1.2 Added the component parameter
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Post by djwk »

Any clue on what I can do then?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Use the normal output. Get the element(s) you wish from there.
Post Reply