How to convert an url in a link? I have some strings that can contain urls inside, and I need to convert this urls into links.

For example if a have this string:

Read for more information www.drupal.stackexchange.com/q/246298/28275


I need to obtain this string:

Read for more information &lt;a href="http://www.drupal.stackexchange.com/q/246298/28275"&gt;www.drupal.stackexchange.com/q/246298/28275&lt;/a&gt;


How can I do this?
To achieve this in Drupal 8 we have in the Core the filter_url filter that convert URLs into links.

So we just need to apply the filter to our string.

use Drupal\filter\FilterPluginCollection;

// The text processing filters service.
$manager = \Drupal::service('plugin.manager.filter');
// Getting filter plugin collection.
$filter_collection = new FilterPluginCollection($manager, []);
// Getting the filter_url plugin.
$filter = $filter_collection-&gt;get('filter_url');
// Setting the filter_url plugin configuration.
$filter-&gt;setConfiguration([
  'settings' =&gt; [
     'filter_url_length' =&gt; 496,
   ]
]);
// The string.
$input = 'Read for more information www.drupal.stackexchange.com/q/246298/28275';
// Applying the filter.
$result = _filter_url($input, $filter);

// To use dpm() you need the devel module.
dpm($result);


For Drupal 7 is more simple

// Creating the filter.
$filter = new stdClass();
// Adding the filter settings.
$filter-&gt;settings = array(
  'filter_url_length' =&gt; 496,
);
// The string.
$input = 'Read for more information www.drupal.stackexchange.com/q/246298/28275';
// Applying the filter.
$result = _filter_url($input, $filter);

// To use dpm() you need the devel module.
dpm($result);


Output in both examples:

Read for more information &lt;a href="http://www.drupal.stackexchange.com/q/246298/28275"&gt;www.drupal.stackexchange.com/q/246298/28275&lt;/a&gt;