本文整理汇总了PHP中utf8_substr_replace函数的典型用法代码示例。如果您正苦于以下问题:PHP utf8_substr_replace函数的具体用法?PHP utf8_substr_replace怎么用?PHP utf8_substr_replace使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了utf8_substr_replace函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: autoTag
/**
* Inserts tag links into an HTML-formatted text.
*
* @param string $html
* @param array $tags
* @param array $options
* @return string
*/
public static function autoTag($html, array $tags, array &$options = array())
{
if (empty($tags)) {
return $html;
}
$html = strval($html);
$htmlNullified = utf8_strtolower($html);
$htmlNullified = preg_replace_callback('#<a[^>]+>.+?</a>#', array(__CLASS__, '_autoTag_nullifyHtmlCallback'), $htmlNullified);
$htmlNullified = preg_replace_callback('#<[^>]+>#', array(__CLASS__, '_autoTag_nullifyHtmlCallback'), $htmlNullified);
// prepare the options
$onceOnly = empty($options['onceOnly']) ? false : true;
$options['autoTagged'] = array();
// reset this
// sort tags with the longest one first
// since 1.0.3
usort($tags, array(__CLASS__, '_autoTag_sortTagsByLength'));
foreach ($tags as $tag) {
$offset = 0;
$tagText = utf8_strtolower($tag['tag']);
$tagLength = utf8_strlen($tagText);
while (true) {
$pos = utf8_strpos($htmlNullified, $tagText, $offset);
if ($pos !== false) {
// the tag has been found
if (self::_autoTag_hasValidCharacterAround($html, $pos, $tagText)) {
// and it has good surrounding characters
// start replacing
$displayText = utf8_substr($html, $pos, $tagLength);
$template = new XenForo_Template_Public('tinhte_xentag_bb_code_tag_tag');
$template->setParam('tag', $tag);
$template->setParam('displayText', $displayText);
$replacement = $template->render();
if (strlen($replacement) === 0) {
// in case template system hasn't been initialized
$replacement = sprintf('<a href="%s">%s</a>', XenForo_Link::buildPublicLink('tags', $tag), $displayText);
}
$html = utf8_substr_replace($html, $replacement, $pos, $tagLength);
$htmlNullified = utf8_substr_replace($htmlNullified, str_repeat('_', utf8_strlen($replacement)), $pos, $tagLength);
// sondh@2012-09-20
// keep track of the auto tagged tags
$options['autoTagged'][$tagText][$pos] = $replacement;
$offset = $pos + utf8_strlen($replacement);
if ($onceOnly) {
// auto link only once per tag
// break the loop now
break;
// while (true)
}
} else {
$offset = $pos + $tagLength;
}
} else {
// no match has been found, stop working with this tag
break;
// while (true)
}
}
}
return $html;
}
示例2: utf8_ucwords_callback
/**
* Callback function for preg_replace_callback call in utf8_ucwords
* You don't need to call this yourself
* @param array of matches corresponding to a single word
* @return string with first char of the word in uppercase
* @see utf8_ucwords
* @see utf8_strtoupper
* @package utf8
* @subpackage strings
*/
function utf8_ucwords_callback($matches)
{
$leadingws = $matches[2];
$ucfirst = utf8_strtoupper($matches[3]);
$ucword = utf8_substr_replace(ltrim($matches[0]), $ucfirst, 0, 1);
return $leadingws . $ucword;
}
示例3: update
public static function update($targetClass, $targetPath, $sourceClass, $sourcesContents)
{
$targetContents = str_replace($sourceClass, $targetClass, $sourcesContents);
$php = '<?php';
$pos = utf8_strpos($targetContents, $php);
if ($pos !== false) {
$replacement = sprintf("%s\n\n// updated by %s at %s", $php, __CLASS__, date('c'));
$targetContents = utf8_substr_replace($targetContents, $replacement, $pos, utf8_strlen($php));
}
$classPrefix = substr($targetClass, 0, strpos($targetClass, 'ShippableHelper_'));
$offset = 0;
while (true) {
if (!preg_match('#DevHelper_Helper_ShippableHelper_[a-zA-Z_]+#', $targetContents, $matches, PREG_OFFSET_CAPTURE, $offset)) {
break;
}
$siblingSourceClass = $matches[0][0];
$offset = $matches[0][1];
$siblingTargetClass = str_replace('DevHelper_Helper_', $classPrefix, $siblingSourceClass);
$targetContents = substr_replace($targetContents, $siblingTargetClass, $offset, strlen($siblingSourceClass));
class_exists($siblingTargetClass);
$offset += 1;
}
$targetContents = preg_replace('#\\* @version \\d+\\s*\\n#', '$0 * @see ' . $sourceClass . "\n", $targetContents, -1, $count);
return DevHelper_Generator_File::filePutContents($targetPath, $targetContents);
}
示例4: truncate
function truncate($text, $length = 30, $truncateString = '...')
{
if (utf8_strlen($text) > $length) {
return utf8_substr_replace($text, $truncateString, $length - utf8_strlen($truncateString));
} else {
return $text;
}
}
示例5: renderTagUrl
public function renderTagUrl(array $tag, array $rendererStates)
{
if (!empty($tag['option'])) {
$url = $tag['option'];
$text = $this->renderSubTree($tag['children'], $rendererStates);
} else {
$url = $this->stringifyTree($tag['children']);
$text = urldecode($url);
if (!preg_match('/./u', $text)) {
$text = $url;
}
$text = XenForo_Helper_String::censorString($text);
if (!empty($rendererStates['shortenUrl'])) {
$length = utf8_strlen($text);
if ($length > 100) {
$text = utf8_substr_replace($text, '...', 35, $length - 35 - 45);
}
}
$text = htmlspecialchars($text);
}
$url = $this->_getValidUrl($url);
if (!$url) {
return $text;
} else {
list($class, $target, $type) = XenForo_Helper_String::getLinkClassTarget($url);
$class = $class ? " class=\"{$class}\"" : '';
$target = $target ? " target=\"{$target}\"" : '';
if ($type == 'internal') {
$noFollow = '';
} else {
$noFollow = empty($rendererStates['noFollowDefault']) ? '' : ' rel="nofollow"';
}
$url = XenForo_Helper_String::censorString($url);
$test = $this->isImage($url);
if ($test) {
return sprintf($this->_imageTemplate, htmlspecialchars($url), $rendererStates['lightBox'] ? ' LbImage' : '');
}
return $this->_wrapInHtml('<a href="' . htmlspecialchars($url) . '"' . $target . $class . $noFollow . '>', '</a>', $text);
}
}
示例6: renderTagUrl
/**
* Renders a URL tag.
*
* @param array $tag Information about the tag reference; keys: tag, option, children
* @param array $rendererStates Renderer states to push down
*
* @return string Rendered tag
*/
public function renderTagUrl(array $tag, array $rendererStates)
{
if (!empty($tag['option'])) {
$url = $tag['option'];
$text = $this->renderSubTree($tag['children'], $rendererStates);
} else {
$url = $this->stringifyTree($tag['children']);
$text = rawurldecode($url);
if (!preg_match('/./u', $text)) {
$text = $url;
}
$text = XenForo_Helper_String::censorString($text);
if (!empty($rendererStates['shortenUrl'])) {
$length = utf8_strlen($text);
if ($length > 100) {
$text = utf8_substr_replace($text, '...', 35, $length - 35 - 45);
}
}
$text = htmlspecialchars($text);
}
$url = $this->_getValidUrl($url);
if (!$url) {
return $text;
} else {
list($class, $target, $type) = XenForo_Helper_String::getLinkClassTarget($url);
if ($type == 'internal') {
$noFollow = '';
} else {
$noFollow = empty($rendererStates['noFollowDefault']) ? '' : ' rel="nofollow"';
}
$href = XenForo_Helper_String::censorString($url);
if ($rendererStates['disableProxying']) {
$proxyHref = false;
} else {
$proxyHref = $this->_handleLinkProxyOption($href, $type);
}
$proxyAttr = '';
if ($proxyHref) {
$proxyAttr = ' data-proxy-href="' . htmlspecialchars($proxyHref) . '"';
$class .= ' ProxyLink';
}
$class = $class ? " class=\"{$class}\"" : '';
$target = $target ? " target=\"{$target}\"" : '';
return $this->_wrapInHtml('<a href="' . htmlspecialchars($href) . '"' . $target . $class . $proxyAttr . $noFollow . '>', '</a>', $text);
}
}
示例7: spell_check
/**
* Spellchecker. Called by an AJAX request
*
* Runs the given Text through Aspell and prints XHTML with
* markup. The first char represents the error code:
*
* 0 - No spelling mistakes
* 1 - Spelling mistakes found
* 2 - An error occurred error message follows
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function spell_check()
{
global $spell;
$string = $_POST['data'];
$misspell = false;
// for streamlined line endings
$string = preg_replace("/(\r\n)|(\r)/", "\n", $string);
$string = htmlspecialchars($string);
// make sure multiple spaces and leading are kept
$string = preg_replace('/^ /m', ' ', $string);
$string = preg_replace('/ /', ' ', $string);
// we need the text as array later
$data = explode("\n", $string);
// don't check links and medialinks for spelling errors
$string = preg_replace_callback('/\\{\\{(.*?)(\\|(.*?))?(\\}\\})/', 'spaceslink', $string);
$string = preg_replace_callback('/\\[\\[(.*?)(\\|(.*?))?(\\]\\])/', 'spaceslink', $string);
// run aspell in terse sgml mode, ignore nbsp as correct word
if (!$spell->runAspell($string, $out, $err, array('!', '+html', '@nbsp'))) {
print '2';
//to indicate an error
print "An error occurred while trying to run the spellchecker:\n";
print $err;
return;
}
#use this to debug raw aspell output
#print "1$out"; return;
// go through the result
$lines = split("\n", $out);
$rcnt = count($lines) - 1;
// aspell result count
$lcnt = count($data) + 1;
// original line counter
for ($i = $rcnt; $i >= 0; $i--) {
$line = trim($lines[$i]);
if ($line[0] == '@') {
continue;
}
// comment
if ($line[0] == '*') {
continue;
}
// no mistake in this word
if ($line[0] == '+') {
continue;
}
// root of word was found
if ($line[0] == '?') {
continue;
}
// word was guessed
if (empty($line)) {
// empty line -> new source line
$lcnt--;
continue;
}
// now get the misspelled words
if (preg_match('/^& ([^ ]+) (\\d+) (\\d+): (.*)/', $line, $match)) {
// match with suggestions
$word = $match[1];
$off = $match[3] - 1;
$sug = split(', ', $match[4]);
} elseif (preg_match('/^# ([^ ]+) (\\d+)/', $line, $match)) {
// match without suggestions
$word = $match[1];
$off = $match[2] - 1;
$sug = null;
} else {
// couldn't parse output
print '2';
print "The spellchecker output couldn't be parsed.\n";
print "Line {$i}:" . $line;
return;
}
$misspell = true;
//aspell < 0.60 returns singlebyte offsets
if ($spell->version >= 600) {
$len = utf8_strlen($word);
$data[$lcnt] = utf8_substr_replace($data[$lcnt], spell_formatword($word, $sug), $off, $len);
} else {
$len = strlen($word);
$data[$lcnt] = substr_replace($data[$lcnt], spell_formatword($word, $sug), $off, $len);
}
}
//end of output parsing
// the first char returns the spell info
if ($misspell) {
$string = '1' . join('<br />', $data);
} else {
//.........这里部分代码省略.........
示例8: autoTag
/**
* Inserts tag links into an HTML-formatted text.
*
* @param string $html
* @param array $tags
* @param array $options
*/
public static function autoTag($html, array $tagsOrTexts, array &$options = array())
{
if (empty($tagsOrTexts)) {
return $html;
}
$html = strval($html);
$tagTexts = Tinhte_XenTag_Helper::getTextsFromTagsOrTexts($tagsOrTexts);
// prepare the options
$onceOnly = empty($options['onceOnly']) ? false : true;
$options['autoTagged'] = array();
// reset this
// sort tags with the longest one first
// since 1.0.3
usort($tagTexts, array(__CLASS__, '_autoTag_sortTagsByLength'));
foreach ($tagTexts as $tagText) {
$offset = 0;
$tagLength = utf8_strlen($tagText);
while (true) {
$pos = Tinhte_XenTag_Helper::utf8_stripos($html, $tagText, $offset);
if ($pos !== false) {
// the tag has been found
if (!self::_autoTag_isBetweenHtmlTags($html, $pos) and self::_autoTag_hasValidCharacterAround($html, $pos, $tagText)) {
// and it's not between HTML tags,
// with good surrounding characters
// start replacing
$template = new XenForo_Template_Public('tinhte_xentag_bb_code_tag_tag');
$template->setParam('tag', $tagText);
$template->setParam('displayText', utf8_substr($html, $pos, $tagLength));
$replacement = $template->render();
$html = utf8_substr_replace($html, $replacement, $pos, $tagLength);
// sondh@2012-09-20
// keep track of the auto tagged tags
$options['autoTagged'][$tagText][$pos] = $replacement;
$offset = $pos + utf8_strlen($replacement);
if ($onceOnly) {
// auto link only once per tag
// break the loop now
break;
// while (true)
}
} else {
$offset = $pos + $tagLength;
}
} else {
// no match has been found, stop working with this tag
break;
// while (true)
}
}
}
return $html;
}
示例9: snippet
/**
* @param string $string
* @param int $maxLength
* @param array $options
*
* @return string
*/
public static function snippet($string, $maxLength = 0, array $options = array())
{
$options = array_merge(array('previewBreakBbCode' => 'prbreak', 'ellipsis' => '…'), $options);
if (!empty($options['previewBreakBbCode']) && preg_match(sprintf('#\\[%1$s\\](?<preview>.*)\\[/%1$s\\]#', preg_quote($options['previewBreakBbCode'], '#')), $string, $matches, PREG_OFFSET_CAPTURE)) {
// preview break bbcode found
if (!empty($matches['preview'][0])) {
// preview text specified, use it directly
$string = $matches['preview'][0];
$maxLength = 0;
} else {
// use content before the found bbcode to continue
$string = substr($string, 0, $matches[0][1]);
$maxLength = 0;
}
}
$snippet = XenForo_Template_Helper_Core::callHelper('snippet', array($string, $maxLength, $options));
// TODO: find better way to avoid having to call this
$snippet = htmlspecialchars_decode($snippet);
if ($maxLength == 0 || $maxLength > utf8_strlen($string)) {
return $snippet;
}
$offset = 0;
$stack = array();
while (true) {
$startPos = utf8_strpos($snippet, '<', $offset);
if ($startPos !== false) {
$endPos = utf8_strpos($snippet, '>', $startPos);
if ($endPos === false) {
// we found a partial open tag, best to delete the whole thing
$snippet = utf8_substr($snippet, 0, $startPos) . $options['ellipsis'];
break;
}
$foundLength = $endPos - $startPos - 1;
$found = utf8_substr($snippet, $startPos + 1, $foundLength);
$offset = $endPos;
if (preg_match('#^(?<closing>/?)(?<tag>\\w+)#', $found, $matches)) {
$tag = $matches['tag'];
$isClosing = !empty($matches['closing']);
$isSelfClosing = !$isClosing && utf8_substr($found, $foundLength - 1, 1) === '/';
if ($isClosing) {
$lastInStack = null;
if (count($stack) > 0) {
$lastInStack = array_pop($stack);
}
if ($lastInStack !== $tag) {
// found tag does not match the one in stack
$replacement = '';
// first we have to close the one in stack
if ($lastInStack !== null) {
$replacement .= sprintf('</%s>', $tag);
}
// then we have to self close the found tag
$replacement .= utf8_substr($snippet, $startPos, $endPos - $startPos - 1);
$replacement .= '/>';
// do the replacement
$snippet = utf8_substr_replace($snippet, $replacement, $startPos, $endPos - $startPos);
$offset = $startPos + utf8_strlen($snippet);
}
} elseif ($isSelfClosing) {
// do nothing
} else {
// is opening tag
$stack[] = $tag;
}
}
} else {
break;
}
}
while (!empty($stack)) {
$snippet .= sprintf('</%s>', array_pop($stack));
}
$snippet = utf8_trim($snippet);
if ($snippet === '') {
// this is bad...
// happens if the $maxLength is too low and for some reason the very first tag cannot finish
$snippet = utf8_trim(strip_tags($string));
if ($snippet !== '') {
$snippet = XenForo_Template_Helper_Core::callHelper('snippet', array($snippet, $maxLength, $options));
} else {
// this is super bad...
// the string is one big html tag and it is too damn long
$snippet = $options['ellipsis'];
}
}
return $snippet;
}
示例10: pun_tags_parse_string
function pun_tags_parse_string($text)
{
global $lang_pun_tags;
if (utf8_strlen(forum_trim($text)) > 100) {
message($lang_pun_tags['Count error']);
}
// Remove symbols and multiple whitespace
$text = preg_replace('/[\'\\^\\$&\\(\\)<>`"\\|@_\\?%~\\+\\[\\]{}:=\\/#\\\\;!\\*\\.]+/', '', preg_replace('/[\\s]+/', ' ', $text));
$text = censor_words($text);
$text = explode(',', $text);
$results = array();
foreach ($text as $tag) {
$tmp_tag = utf8_trim($tag);
if (!empty($tmp_tag)) {
$results[] = utf8_substr_replace($tmp_tag, '', 50);
}
}
return array_unique($results);
}
示例11: substr_replace
public static function substr_replace($str, $repl, $start, $length = null)
{
if ($length === false) {
return utf8_substr_replace($str, $repl, $start);
} else {
return utf8_substr_replace($str, $repl, $start, $length);
}
}
示例12: testLinefeedReplace
function testLinefeedReplace()
{
$str = "Iñ\ntërnâtiônàlizætiøn";
$replaced = "Iñ\ntërnâtX\nY";
$this->assertEqual(utf8_substr_replace($str, "X\nY", 9), $replaced);
}
示例13: getBlogsReviews
//.........这里部分代码省略.........
$thumb = '';
}
if (!isset($comment['text'])) {
$comment['text'] = '';
}
$text = '';
if ($comment['text'] != '') {
$flag_desc = 'none';
if ($thislist['desc_symbols'] != '') {
$amount = $thislist['desc_symbols'];
$flag_desc = 'symbols';
}
if ($thislist['desc_words'] != '') {
$amount = $thislist['desc_words'];
$flag_desc = 'words';
}
if ($thislist['desc_pred'] != '') {
$amount = $thislist['desc_pred'];
$flag_desc = 'pred';
}
//if ($flag_desc != 'none')
//$comment['text'] = preg_replace('/\[(.*?)\]/', '', $comment['text']);
switch ($flag_desc) {
case 'symbols':
$limit = $amount;
$source = strip_tags(html_entity_decode($comment['text'], ENT_QUOTES, 'UTF-8'));
$counter = 0;
$matches = array();
utf8_preg_match_all('/(?:\\[.*\\].*\\[\\/.*\\])|(.)/Usiu', $source, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[1] as $num => $val) {
if (is_array($val)) {
$counter++;
if ($counter == $limit) {
$source = utf8_substr_replace($source, '', $val[1] + 1);
break;
}
}
}
$text = $source;
//$pattern = ('/((.*?)\S){0,' . $amount . '}/isu');
//preg_match_all($pattern, strip_tags(html_entity_decode($comment['text'], ENT_QUOTES, 'UTF-8')), $out);
//$text = $out[0][0];
break;
case 'words':
$limit = $amount;
$source = strip_tags(html_entity_decode($comment['text'], ENT_QUOTES, 'UTF-8'));
$counter = 0;
$matches = array();
utf8_preg_match_all('/(?:\\[.*\\].*\\[\\/.*\\])|(\\x20)/Usiu', $source, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[1] as $num => $val) {
if (is_array($val)) {
$counter++;
if ($counter == $limit) {
$source = utf8_substr_replace($source, '', $val[1] + 1);
break;
}
}
}
$text = $source;
/*
$pattern = ('/((.*?)\x20){0,' . $amount . '}/isu');
preg_match_all($pattern, strip_tags(html_entity_decode($comment['text'], ENT_QUOTES, 'UTF-8')), $out);
$text = $out[0][0];*/
break;
case 'pred':
$limit = $amount;
示例14: substr_replace
/**
* UTF-8 aware substr_replace
* Replace text within a portion of a string
*
* @param string $str The haystack
* @param string $repl The replacement string
* @param integer $start Start
* @param integer $length Length (optional)
*
* @return string
*
* @see http://www.php.net/substr_replace
* @since 11.1
*/
public static function substr_replace($str, $repl, $start, $length = null)
{
// Loaded by library loader
if ($length === false) {
return utf8_substr_replace($str, $repl, $start);
} else {
return utf8_substr_replace($str, $repl, $start, $length);
}
}
示例15: substr_replace
/**
* UTF-8 aware substr_replace
* Replace text within a portion of a string
*
* @static
* @access public
* @param string the haystack
* @param string the replacement string
* @param int start
* @param int length (optional)
* @see http://www.php.net/substr_replace
*/
public static function substr_replace($str, $repl, $start, $length = NULL)
{
// loaded by library loader
if ($length === FALSE) {
return utf8_substr_replace($str, $repl, $start);
} else {
return utf8_substr_replace($str, $repl, $start, $length);
}
}