Skip to content

Instantly share code, notes, and snippets.

@alrnz
Created April 26, 2016 14:21
Show Gist options
  • Save alrnz/36cccd1ceea414215f9a85699f1e4b9b to your computer and use it in GitHub Desktop.
Save alrnz/36cccd1ceea414215f9a85699f1e4b9b to your computer and use it in GitHub Desktop.
TextlinkViewHelper
<?php
namespace Sunzinet\SzNewsroom\ViewHelpers;
/**
* This file is part of the TYPO3 CMS project.
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
* The TYPO3 project - inspiring people to share!
* (c) 2016 Arne Lorenz (al@sunzinet.com)
*/
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Replaces URLs in a Text with Links
* Example:
* {namespace newsrooom=Sunzinet\SzNewsroom\ViewHelpers}
* <newsrooom:textlink>Text comes here</newsrooom:textlink>
*
* @author Arne Lorenz <al@sunzinet.com>
*/
class TextlinkViewHelper extends AbstractViewHelper
{
/**
* @return string
*/
public function render()
{
$content = $this->renderChildren();
$formatted_text = preg_replace(
'/(\b(www\.|http\:\/\/|https\:\/\/)\S+\b)/',
"<a target='_blank' href='$1'>$1</a>",
$content
);
return $formatted_text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment