1

I am getting data from a page like this:

<?php $speaker = $page->uri();
  $speakerEvents = page('program')->grandchildren()->filter(function($child) use($speaker) {
  $speakers = $child->speaker()->toStructure();
  return $speakers->findBy('selectspeaker', $speaker);
});

echo $speakerEvents;                

?>

It's output is:

"page/page/pageIWant
page/page/pageIWant"

The Result I want is

pageIWant
pageIWant

I tried to get the last name with

echo basename($speakerEvents);

But I only get one of the last pageIWant

How do I get the last pages without removing the first URL?

Dennis
  • 137
  • 1
  • 2
  • 14
  • 3
    Possible duplicate of [Get Last Part of URL PHP](https://stackoverflow.com/questions/7395049/get-last-part-of-url-php) – Anand Mainali Jan 29 '19 at 16:40

3 Answers3

2

Use the explode method to get array of URL

<?php
    $link = $_SERVER['PHP_SELF'];
    $link_array = explode('/',$link);
    echo $page = end($link_array);
?>

If your output is

page/page/pageIWant
page/page/pageIWant

<?php
    $text = "page/page/pageIWant";
    $text_arr = explode('/',$text);
    echo $page = end($text_arr);
    // this will print: pageIWant
?>
Googlian
  • 6,077
  • 3
  • 38
  • 44
2

Your string has a line break as path separator. You need to split lines by either \r\n (CR-LF), \n, or \r which can be done with preg_split. If you can ensure one of the formats, you can also use the simple explode function.

foreach (preg_split('~(?:\r?\n|\r)~u', $speakerEvents) as $path)
  echo basename($path) , PHP_EOL;

As AbraCadaver noted, you can add the flag PREG_SPLIT_NO_EMPTY to preg_split if you do not want empty lines to be handled.

If the double qoutes you show in the question are part of the string, you need further more to trim the string:

foreach (preg_split('~(?:\r?\n|\r)~u', trim($speakerEvents, '"')) as $path)
  echo basename($path) , PHP_EOL;
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
1

You can explode the string e.g.

$array = explode('/', 'page/page/pageIWant');

Then you can retrieve it using the index like so:

$array[(count($array) - 1)];

  • Thanks for your fast answer I tried it like this: `$array = explode('/', $speakerEvents);echo $array[(count($array) - 1)];` Unfortunately I still get only one last `pageIWant` – Dennis Jan 29 '19 at 16:26
  • You've made two lines one string in your question, should that be two strings or you need the third and last item? If it's the same format every time, you can simply just use index's 2 and 5. –  Jan 29 '19 at 16:28
  • so it will be the same format every time. On some pages it will be more lines and on some it will be less. Could you help me here? – Dennis Jan 29 '19 at 16:30
  • At the time of the code executing, do you know the name of the pages you want? Because you could always loop through the array and check with a simple [foreach loop](http://php.net/manual/en/control-structures.foreach.php) –  Jan 29 '19 at 16:32
  • of course! Dump! Its now working as I expected with a loop. – Dennis Jan 29 '19 at 16:36