Skip to content

Instantly share code, notes, and snippets.

@mermshaus
Created April 13, 2013 12:25
Show Gist options
  • Save mermshaus/5378193 to your computer and use it in GitHub Desktop.
Save mermshaus/5378193 to your computer and use it in GitHub Desktop.
addUrlTagsToTextFilter
<?php
function addUrlTagsToTextFilter($text)
{
// Positionen aller url-BBCode-Elemente ermitteln
$matches = array();
preg_match_all('~\[url(?:.*?)\](?:.*?)\[/url\]~', $text, $matches, PREG_OFFSET_CAPTURE);
$bbCodeElements = $matches[0];
// Positionen aller URLs ermitteln
$matches2 = array();
preg_match_all('~(?:http|https|ftp)://[^\s\[\]]+~', $text, $matches2, PREG_OFFSET_CAPTURE);
$urls = $matches2[0];
// Prüfen, ob eine Position innerhalb eines url-BBCode-Elements liegt
$isInBbCodeElement = function ($pos) use ($bbCodeElements) {
$ret = false;
foreach ($bbCodeElements as $element) {
$elementStartPos = $element[1];
$elementLen = strlen($element[0]);
if ($pos >= $elementStartPos && $pos < $elementStartPos + $elementLen) {
$ret = true;
break;
}
}
return $ret;
};
$output = '';
$pos = 0;
// Alle URLs durchgehen und diejenigen, deren Position nicht in einem
// url-BBCode-Element liegt, mit [url]...[/url] umgeben. Dabei den übrigen
// Nicht-URL-Text entsprechend ergänzen
foreach ($urls as $urlMatch) {
$urlStartPos = $urlMatch[1];
$urlLen = strlen($urlMatch[0]);
$output .= substr($text, $pos, $urlStartPos - $pos);
if ($isInBbCodeElement($urlStartPos)) {
$output .= $urlMatch[0];
} else {
$output .= '[url]' . $urlMatch[0] . '[/url]';
}
$pos = $urlStartPos + $urlLen;
}
$output .= substr($text, $pos);
return $output;
}
$tests = array();
$tests[] = array(
'input' => '',
'expectedOutput' => ''
);
$tests[] = array(
'input' => ' ',
'expectedOutput' => ' '
);
$tests[] = array(
'input' => 'http://www.google.de/',
'expectedOutput' => '[url]http://www.google.de/[/url]'
);
$tests[] = array(
'input' => 'foo http://www.google.de/',
'expectedOutput' => 'foo [url]http://www.google.de/[/url]'
);
$tests[] = array(
'input' => 'http://www.google.de/ foo',
'expectedOutput' => '[url]http://www.google.de/[/url] foo'
);
$tests[] = array(
'input' => '[url]http://www.google.de/[/url]',
'expectedOutput' => '[url]http://www.google.de/[/url]'
);
$tests[] = array(
'input' => 'foo [url]http://www.google.de/[/url]',
'expectedOutput' => 'foo [url]http://www.google.de/[/url]'
);
$tests[] = array(
'input' => '[url]http://www.google.de/[/url] foo',
'expectedOutput' => '[url]http://www.google.de/[/url] foo'
);
$tests[] = array(
'input' => 'foo [url=http://www.google.de/]test[/url] bar',
'expectedOutput' => 'foo [url=http://www.google.de/]test[/url] bar'
);
$tests[] = array(
'input' => 'foo [url=http://www.google.de/]test[/url]http://example.org/ bar',
'expectedOutput' => 'foo [url=http://www.google.de/]test[/url][url]http://example.org/[/url] bar'
);
$tests[] = array(
'input' => 'foo http://example.org/[url=http://www.google.de/]test[/url] bar',
'expectedOutput' => 'foo [url]http://example.org/[/url][url=http://www.google.de/]test[/url] bar'
);
$tests[] = array(
'input' => <<<'EOT'
bla bla http://google.de/-sdf/asdf
google.de/-sdf/asdf blub
http://www.google.de/-sdf/asdf
www.google.de/-sdf/asdf
lorem ipsum dolor
ipsum [url=http://google.de/-sdf/asdf]test[/url]
[url=http://www.google.de/-sdf/asdf]www.google.de/-sdf/asdf[/url] test
xyz [url=www.google.de/-sdf/asdf]test[/url]
[url]http://www.google.de/-sdf/asdf[/url] hijk
lmno
EOT
,
'expectedOutput' => <<<'EOT'
bla bla [url]http://google.de/-sdf/asdf[/url]
google.de/-sdf/asdf blub
[url]http://www.google.de/-sdf/asdf[/url]
www.google.de/-sdf/asdf
lorem ipsum dolor
ipsum [url=http://google.de/-sdf/asdf]test[/url]
[url=http://www.google.de/-sdf/asdf]www.google.de/-sdf/asdf[/url] test
xyz [url=www.google.de/-sdf/asdf]test[/url]
[url]http://www.google.de/-sdf/asdf[/url] hijk
lmno
EOT
);
foreach ($tests as $index => $test) {
echo 'Test #' . $index . ': ';
if (addUrlTagsToTextFilter($test['input']) === $test['expectedOutput']) {
echo "OK\n";
} else {
echo "ERROR\n";
echo "----------------------\nInput:\n";
echo $test['input'];
echo "\n----------------------\nExpected:\n";
echo $test['expectedOutput'];
echo "\n----------------------\nGiven:\n";
echo addUrlTagsToTextFilter($test['input']);
echo "\n----------------------\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment