Lamez Posted August 19, 2008 Share Posted August 19, 2008 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 More sharing options...
akitchin Posted August 19, 2008 Share Posted August 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620478 Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 wait, so I will need to use substr() first, then strchr()? Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620479 Share on other sites More sharing options...
akitchin Posted August 19, 2008 Share Posted August 19, 2008 no, you'll want to use strrchr() first to strip the last portion off of the string. then use substr() to get rid of that starting forward slash. Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620480 Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 ok, I am going to make a sandwich, and then I will get started on it. Thanks for pointing me in the right direction, also I will want to delete the image later on, do I use unlink() or imagedestory()? Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620481 Share on other sites More sharing options...
akitchin Posted August 19, 2008 Share Posted August 19, 2008 unlink(). imagedestroy(), as it says in the manual, destroys any memory associated with an image. that implies (and it says in the manual) that this is for use with dynamically created images. Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620484 Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 well see that is what I am asking, I did read the manual, but is unlink the best one? Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620491 Share on other sites More sharing options...
akitchin Posted August 19, 2008 Share Posted August 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620497 Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620511 Share on other sites More sharing options...
akitchin Posted August 19, 2008 Share Posted August 19, 2008 you don't need to know how long the string is, as you can simply specify a starting point and chopping length: echo substr('abcdef', 1); // bcdef that chops off the first character and returns the rest. Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620522 Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 oh I get it! Let me see! Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620524 Share on other sites More sharing options...
Lamez Posted August 19, 2008 Author Share Posted August 19, 2008 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; ?> Link to comment https://forums.phpfreaks.com/topic/120420-solved-getting-a-part-of-a-string/#findComment-620528 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.