1

Substr PHP

I have a string like this http://domain.sf/app_local.php/foo/bar/33. The last caracters are the id of an element. His lenght could be more than one so i can not use:

substr($dynamicstring, -1);

In this case must be

substr($dynamicstring, -2);

How can i get the caracters after "/bar/" on the string without depending on the length?

Oscar
  • 1,734
  • 2
  • 15
  • 30

7 Answers7

5

To ensure you are getting immediate section after bar, use Regular Expressions:

preg_match('~/bar/([^/?&#]+)~', $url, $matches);
echo $matches[1]; // 33
revo
  • 45,845
  • 14
  • 70
  • 113
  • 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));

Hope this helps!

Luke
  • 578
  • 7
  • 18
  • 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.

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,515
  • 4
  • 34
  • 63
0

You can use 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];
}
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;
Satizh J
  • 277
  • 4
  • 14
-2

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]);

Hope this helps.

Mirza
  • 150
  • 6
  • Why should OP use count if length should be avoided? – Tom Regner Dec 01 '17 at 12:27
  • 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