• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP - Trim all characters after specific character in string

BMR777

New Member
Hello,

I need to trim all characters after a specific character in a string in PHP. I want to take a string and remove all characters that occur after a slash /.

EX:

PHP:
$mystring = "url/index.php"

//Do Something...

$mystring = "url/"

Any hints or tips? Thanks so much!
BMR777
 
PHP:
$mystring = "url/index.php";

$parts = explode("/",$mystring);
//break the string up around the "/" character in $mystring

$mystring = $parts['0'];
//grab the first part

echo $mystring;
 
or just..

Code:
<?php
$mystring = "url/index.php";
$mystring = substr($mystring, 0, strpos($mystring, "/"));
echo $mystring;
?>

bleh sorry, been coding in VB lately..forgot the $.
 
Last edited:
If it's always a file name do this:
PHP:
<?php
$myurl = "my/folders/go/here/index.php";

$file = basename($myurl);

$parts = explode(".", $file);

//File name
echo $parts[0];

//File format
echo $parts[1];
?>
 
JohnN, your code works perfectly for my application. Thanks so much!

Everyone, thanks for your help!
BMR777
 
Back
Top