1

Substr PHP

I have a string like http://domain.sf/app_local.php/foo/bar/33.

The last characters are the id of an element. Its length could be more than one, so I can not use:

substr($dynamicstring, -1);

In this case, it must be:

substr($dynamicstring, -2);

How can I get the characters after "/bar/" on the string without depending on the length?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oscar
  • 1,929
  • 3
  • 16
  • 31

7 Answers7

5

To ensure you are getting an immediate section after the bar, use regular expressions:

preg_match('~/bar/([^/?&#]+)~', $url, $matches);
echo $matches[1]; // 33
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
revo
  • 47,783
  • 14
  • 74
  • 117
  • 3
    Easily the best solution here, the others all ignore the possibility of GET data or make assumptions about the ID. – Scoots Dec 01 '17 at 12:21
  • i need to initialize `$matches` ? I get `$matches = {array}[0]` so it dont works to me... Im doing something wrong? – Oscar Dec 01 '17 at 12:44
  • 1
    You don't need to initialize it. Are you doing something other than [**this**](https://3v4l.org/71njj)? @Oscar – revo Dec 01 '17 at 13:15
2

You could use explode('/', $dynamicstring) to split the string into an array of the strings inbetween each /. Then you could use end() on the result of this to get the last part.

$id = end(explode('/', $dynamicstring));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luke
  • 1,060
  • 8
  • 19
  • 1
    This won't work because end is expecting an array, as a reference. – Zlatan Omerović Dec 01 '17 at 12:21
  • 3
    @omerowitz It works, it just triggers a notice as well. The return from `explode` should definitley be assigned to an intermediate variable before being passed to `end` to avoid this. – Scoots Dec 01 '17 at 12:24
  • As I've did and explained in my answer. – Zlatan Omerović Dec 01 '17 at 12:25
  • I have the next output trying your answer: `Only variables should be passed by reference` But yes, it helps. I can use explode and then get the end of array but ignore the possibility of the GET data – Oscar Dec 01 '17 at 12:36
2

You can use explode(), like this:

$id = explode('/',$var);

And take the element where you had the id.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Try this:

$dynamicstring = 'http://domain.sf/app_local.php/foo/bar/33';

// split your string into an array with /
$parts = explode('/', $dynamicstring);

// move the array pointer to the end
end($parts);

// return the current position/value of the $parts array
$id = current($parts);

// reset the array pointer to the beginning => 0
// if you want to do any further handling
reset($parts);

echo $id;
// $id => 33

Test it yourself here.

Zlatan Omerović
  • 3,863
  • 4
  • 39
  • 67
0

You can use a regular expression to do it:

$dynamicstring = "http://domain.sf/app_local.php/foo/bar/33";
if (preg_match('#/([0-9]+)$#', $dynamicstring, $m)) {
    echo $m[1];
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrey Yerokhin
  • 273
  • 1
  • 7
0

I tested it out myself before answering. Other answers are reasonable too, but this will work according to your need...

<?php
    $url = "http://domain.sf/app_local.php/foo/bar/33";
    $id = substr($url, strpos($url, "/bar/") + 5);
    echo $id;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Satizh J
  • 307
  • 4
  • 14
-3

Please find the below answer.

$str = "http://domain.sf/app_local.php/foo/bar/33";
$splitArr = explode('/', explode('//', $str)[1]);
var_dump($splitArr[count($splitArr)-1]);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mirza
  • 148
  • 6
  • 1
    Why should OP use count if length should be avoided? – Tom Regner Dec 01 '17 at 12:27
  • 1
    My dearest lord... For which reasons are you exploding by `/` then by `//`, and getting the rest of after second explode... then `count - 1`? What happends with this when instead of full URL I provide just a pathname? – Zlatan Omerović Dec 01 '17 at 12:31