emporio emporio - 6 months ago 14
PHP Question

Converting a DOM element into a string in PHP

I am running a Wordpress site and using WPML translation plugin. In WPML there is a method called

icl_link_to_element(2880);
that returns and generates the following element in the DOM:

<a href="https://www.someurl.com/es/atencion-al-cliente">Atención al cliente</a>


I want to convert this element to a string so that I can simply return and store the last part of the url
atencion-al-cliente
, into a unique variable.

What's the best way to get this done?

Answer to the comment below:

Two elements appear in the DOM now.

<a href="https://www.someurl.com/es/atencion-al-cliente">Atención al cliente</a>


and

"NULL
"

Answer

First do the following steps to get that URL

  1. $page = icl_object_id(2880, 'page', true);
  2. $url = get_permalink($page);

Then use $parts = explode("/", $url) function and get the last element of that array, you can use array_pop()

Comments