Page 1 of 1

Remove Anything After The Forward Slash

Posted: Wed Feb 21, 2007 3:57 am
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?

Posted: Wed Feb 21, 2007 4:25 am
by dhrosti
Im not sure, but you could use $_SERVER['HTTP_HOST']; to get "www.google.com" straight away.

Posted: Wed Feb 21, 2007 5:02 am
by djwk
thanks, did the job :)

Posted: Wed Feb 21, 2007 5:25 am
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.

Posted: Wed Feb 21, 2007 6:06 am
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.

Posted: Wed Feb 21, 2007 8:31 am
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.

Posted: Wed Feb 21, 2007 8:43 am
by feyd
There's an example on the documentation page linked.

Posted: Wed Feb 21, 2007 9:14 am
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

Posted: Wed Feb 21, 2007 9:17 am
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

Posted: Wed Feb 21, 2007 10:11 am
by djwk
Any clue on what I can do then?

Posted: Wed Feb 21, 2007 10:27 am
by feyd
Use the normal output. Get the element(s) you wish from there.