Search code examples
phphtmlsimple-html-dom

get only the second word


When i was parsing (simple html dom.php), I encountered the following problem, when receiving the final data, I need to get only the second word(Barcelona) from the given string.

<h1 class="entry-title">Taxi Barcelona</h1>

On the stack overflow, I found a couple of similar problems, but I would like to analyze this particular case, since using ready-made methods I did not come to success

I already tried this code

  $town = $html->find('h1.entry-title', 0);
  $town_name = $town;
  $t_name =  stripos($town_name, ' ');
  echo substr($town_name, 10);

But it was unsuccessful for my case because it gave the following line: "entry-title">Taxi Barcelona

Can anyone lend me a helping hand?


Solution

  • You need to get the innertext of the element, not use the entire element as a string.

    You can use explode() to split the string into words.

    $town_name = $town->innertext();
    echo explode(' ', $town_name)[1];