Jump to content

[SOLVED] HELP!Removing / Strip the string after .com


jigen7

Recommended Posts

i need help.. suppose i have a variable with a site for example

 

$string = "http://scuba.about.com/cs/snorkelgear/bb/snorkelequip.htm"

 

what i need is i need to remove the string after .com so the remaining string would become like this

 

http://scuba.about.com/

or

http://scuba.about.com

 

what function should i use??

 

thx

 

oki thx it works

 

heres my coding 

 

$action = "http://scuba.about.com/cs/snorkelgear/bb/snorkelequip.htm";

$subpos = 23;

$action = substr($action, 0, $subpos);

echo $action;

 

but still if the site or the site variable  is generated by random how would i know what would be its $subpos??? it would not be always 23 ??

i need help.. suppose i have a variable with a site for example

 

$string = "http://scuba.about.com/cs/snorkelgear/bb/snorkelequip.htm"

 

what i need is i need to remove the string after .com so the remaining string would become like this

 

http://scuba.about.com/

or

http://scuba.about.com

 

also the site variable there is random site so it would not be always http://scuba.about.com/cs/snorkelgear/bb/snorkelequip.htm so i need to determine how to find the .com string what ever site is given then after that i just need to delete the string after the .com

what function should i use??

 

thanks

 

The cleanest way is with parse_url()

 

$action = "http://scuba.about.com/cs/snorkelgear/bb/snorkelequip.htm";
$action_parts = parse_url($action);
var_dump($action_parts);

 

var_dump() will show you the contents of $action_parts, so you can see what's inside and choose which parts to use.  You would remove it in your final code.

oki thx then how do i choose it after the var_dump?? it prints the line

 

array(3) {

  ["scheme"]=>

  string(4) "http"

  ["host"]=>

  string(15) "scuba.about.com"

  ["path"]=>

  string(35) "/cs/snorkelgear/bb/snorkelequip.htm"

}

 

then how can i also hide the txt when using var_dump

ok thx for everyone replying i got the syntax and i made a functin of it here how it goes

 

function strip_com ($value) {

$url = "http://scuba.abocdxcdscdut.com/cs/snorkelgear/bb/snorkelequip.htm";

$findme  = '.com';

$pos = strpos($url, $findme);

 

// Note our use of ===.  Simply == would not work as expected

// because the position of 'a' was the 0th (first) character.

if ($pos === false) {

    echo "The string '$findme' was not found in the string '$mystring' <br>";

} else {

    echo "The string '$findme' was found in the string '$mystring'<br>";

    echo " and exists at position $pos<br>";

}

 

$url = substr($url, 0, $pos+4);

return $url;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.