Jump to content

[SOLVED] Getting a Part of a string?


Lamez

Recommended Posts

ok so I am working on my avatar system, and it encrypts the name, then it add the username with a hyphen at the end.

 

Well that is not the problem I stored it in the database with a path, like this: user/userava/fc60f5d7abc2e080599bb6dc465db54d-JamesLittle.jpg, I was wondering how do I take out the user/userava/ so I just have fc60f5d7abc2e080599bb6dc465db54d-JamesLittle.jpg?

 

-Thanks Guys!

Link to comment
https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/
Share on other sites

have a look at strrchr(), using '/' as the delimiter.  keep in mind that the return from strrchr() will contain the delimiter, so you'll need to substr() to remove the first character.

 

the manual should have the details you need to get it up and running.

unlink is the only one.  imagedestroy() is only for use with dynamically-created images, the likes of which are generated by the built-in image functions in PHP.  assuming your avatars are uploaded (which i believe is a safe assumption) means that unlink() must be used to nix them off of the filesystem.

Oh thank you. I understand that now.

 

Now back to the main topic, I did use strchar, and I get a output like: /fc60f5d7abc2e080599bb6dc465db54d-JamesLittle.jpg

Then I read the manual on substr, like you said, and it was talking about removing parts of the string, but from the example, it looks like you have to know how long the string is, but I don't know how long it is, it can change depending on the username and other randoms.

 

<?php
        $q = mysql_query("SELECT * FROM `users` WHERE `username` = '".$user_."'");
        $qimg = mysql_fetch_array($q);
  $qimg = $qimg['avatar'];
  echo $qimg;
  echo "<br>";
  $out = strrchr($qimg, "/");
  echo $out;
?>

Thank You! You are very helpful!

 

Final code:

    
<?php    
        $q = mysql_query("SELECT * FROM `users` WHERE `username` = '".$user_."'");
        $qimg = mysql_fetch_array($q);
  $qimg = $qimg['avatar'];
  echo $qimg;
  echo "<br>";
  $out = substr(strrchr($qimg, "/"), 1);
  echo $out;
?>

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.