3

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?

Grant
  • 1,297
  • 2
  • 16
  • 39
  • For the last part, you could use **$temp = explode("/",$dataclean); $image = $temp[3];** – Peter Blue Aug 24 '15 at 15:33
  • Or with regex: `$dataclean = preg_replace('~.*/|#.*~', "", $data);` – Jonny 5 Aug 24 '15 at 15:44
  • I've gone for the basename / strpos approach as I read that it is faster than regex and I'm using this function to update a newly created database entry. Not sure how true that statement is or if what I'm doing is a particularly safe thing to do but it seems to be working. – Grant Aug 24 '15 at 15:51
  • @Grant [did a little benchmark](https://eval.in/421770). With 10k loops slowest was the regex (with pcre_study flag bit faster as without) and fasted was pure explode: `$dataclean = explode("#", end(explode("/", $data)))[0];` Almost as fast: The selected answer :] – Jonny 5 Aug 25 '15 at 04:55

5 Answers5

6

Use basename:

$dataclean = basename(substr($data, 0, strpos($data, "#")));
michelem
  • 14,430
  • 5
  • 50
  • 66
1

As mentioned in the question, first remove values after # using

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

Then use basename() function to access filename from the URL.

For example,

    $link = "http://example.com/folderPath/filename.php";
    echo basename($link);  // It will return filename.php
  • This would also return data after the hash. You would need to include removing that in the `basename` function. – camelCase Aug 24 '15 at 15:34
  • 1
    @ride_85027: I merely mentioned the concept with the example itself. The question already contains the solution for removing values after `#` using `substr`. –  Aug 24 '15 at 15:37
1

If basename() doesn't work I would explode the string by the forward slash.

$data = 'image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28';
$pieces = explode("/", $data);

$dataclean = substr($pieces[3], 0, strpos($data, "#"));
H0miet
  • 91
  • 1
  • 10
  • Why `basename` shouldn't work? And that should be `substr($pieces[count($pieces)-1], 0, strpos($data, "#"));` otherwise you need to know every time the array index to get – michelem Aug 24 '15 at 15:43
  • Thanks for adding that tidbit of code. I believe basename is the best answer to this question. I don't know why it wouldn't work but if it doesn't this is another way to possibly do what you need. – H0miet Aug 27 '15 at 21:27
0

Nothing wrong with substr + strpos, but it looks like your string is a URL, so you could use parse_url to isolate the path before using basename. Just another option FYI.

basename(parse_url($yourString, PHP_URL_PATH));
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

You can use a regex function:

preg_match("/[a-z0-9]+\.[a-z]{3}/i", $input_line, $output_array);

Try the code