Newb help - Strip (delete) first character from a string ?

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
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Newb help - Strip (delete) first character from a string ?

Post by MiniMonty »

Hi all,

I need to delete image records (file paths) from a db and then delete the actual image
from a users directory. The way I have set this up (don't ask) means that to delete from the db
I need a path starting with a slash like this:
"/members/1/images/999.jpg"

and to delete the image from the dir I need a path WITHOUT a starting slash like this:
"members/1/images/999.jpg"

The file path is being delivered to the script from a Flash front end as a variable "toPHP" and it arrives WITH a
starting slash. (don't let the Flash bit put you off - it just uses POST to send data and it works fine).

SIMPLE QUESTION:
Having run some code using the string in "toPHP" to delete from the db how can I declare another local variable of
the same string WITHOUT the starting slash to use to delete the actual image ?

My current script attached (which works fine for the db delete WITH the slash and fine for the dir delete WITHOUT it) :D
Best wishes
Monty

Code: Select all

 
// DELETE the record from the db
include_once "scripts/connect_to_mysql.php";
$fromFlash = $_POST['toPHP'];
$returned_message = ($fromFlash);
$sql = mysql_query("DELETE FROM pictures WHERE dirpath = '$fromFlash';")
or die (mysql_error());
 
// Now delete the actual image from the members image folder
if(!unlink($returned_message)) die("Failed to delete file");
    else{
    $toFlash = "&toFlash=";
    $toFlash .= $returned_message;
    echo $toFlash;
}
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newb help - Strip (delete) first character from a string ?

Post by requinix »

substr would be one answer, but based on the question I would say to use ltrim instead.

Code: Select all

$path = ltrim("/members/1/images/999.jpg", "/"); // trim any initial /s
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb help - Strip (delete) first character from a string ?

Post by MiniMonty »

Beautiful.

Just as an aside - is this common ?
I mean is it common to need two different paths to delete from the db and from the dir or have I set things
up badly / newbly / inefficiently ?

Best wishes
Monty
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newb help - Strip (delete) first character from a string ?

Post by requinix »

I wouldn't say it's common, but "bad"? Nah.

Could be a source of confusion in the future, though.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb help - Strip (delete) first character from a string ?

Post by MiniMonty »

Cheers Ears.
(that's "thanks for taking the time" in slang English form England).

Couldn't make sense of the fact that I needed two different file paths to delete from the db and the dir.
And actually I didn't - I just needed different syntax for the two tools to read properly.

Running a Flash front end is great because I can do very cool things graphically (and build a big site very quickly)
but I fall down when it has to talk to then back end and odd anomalies crop up like the slash in the file path.
OOP for Flash? Yes. OOP for php Yes.
In English it's "confusing" and in French it's "confusant". So similar... But the last three letters make it a whole different language !

Heh ho - on we go.

Thanks for your help mate. I spent about an hour trying to get it right before I gave up and posted a question.

Best wishes
Monty

PS - where's "WA"?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newb help - Strip (delete) first character from a string ?

Post by requinix »

MiniMonty wrote:PS - where's "WA"?
Washington (the state), USA.
Post Reply