if (!is_array($positionArray) || !is_array($originalArray)) {
return $originalArray;
}
foreach ($positionArray as $position => $value) {
// position-1 because array_splice position beginns with 0
$position = $position > 0 ? $position-1 : $position;
array_splice($originalArray, $position,
$deleteLength, array($value));
}
if ($maxLength > 0) {
return array_slice($originalArray, 0, $maxLength);
}
return $originalArray;
Shorten a text in consideration of getting full words (PHP)
// Does the text need to be shortened?
if (strlen($text) <= $maxLength) {
return $text;
}
$appendString = '...';
// plus 1, because of prepended space to $appendString
$maxLength = $maxLength - (strlen($appendString)+1);
preg_match("/(.{0,$maxLength}\b)/s", $text, $textShort);
$shortenedText = (isset($textShort[0]) ? $textShort[0] : '')
. ' ' . $appendString;
// if the text has not enough words to reach minLenght,
// shorten hard.
if (!isset($textShort[0]) ||
($minLength !== null &&
strlen($textShort[0]) < $minLength)) {
// minus 1, because of no prepended space to $appendString
$shortenedText = substr($text, 0, $minLength-1)
. $appendString;
Regular Expression: find all links and souround them with an A-tag (PHP)
// Convert links with [protocol]://...
$content = preg_replace('/(((f|ht){1}tp(s)?:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'<a href="\\1"' . $attributesString . '>\\1</a>', $content);
// Convert links without [protocol]://...
$content = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
'\\1<a href="http://\\2"' . $attributesString . '>\\2</a>', $content);
// Convert mail address
$content = preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i',
'<a href="mailto:\\1"' . $attributesString . '>\\1</a>', $content);
Regular Expression: remove double spaces (PHP)
echo trim(preg_replace('/\s{2,}/', ' ', $content);
Java: Datum/Datum-Format Thread-Safe
FastDateFormat is a fast and thread-safe version of SimpleDateFormat.
