Search code examples
phpstringsubstrstrpos

PHP removing several things from a string at once


I have a string like this being entered into my database that I can't format before it gets stored :

image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28

The only part of that string that I want to use is this :

hglz466d8mm1pazysaoh.jpg

I'm trying to use strpos to remove the excess data.

So far I've managed to remove everything after and including the hashtag

($data is the original string) :

$dataclean = substr($data, 0, strpos($data, "#"));

This works as expected with $dataclean returning :

image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg

But I don't know how to remove the rest of the excess data :

image/upload/v1440427262/

Also, can this all be done in one hit or does it have to be split into several operations?


Solution

  • Use basename:

    $dataclean = basename(substr($data, 0, strpos($data, "#")));