示例#1
0
 /**
  * Tests the static strrpos.
  */
 public function test_strrpos()
 {
     $str = "Žluťoučký koníček";
     $this->assertSame(11, core_text::strrpos($str, 'o'));
 }
/**
 * Given the text of a wiki link (between [[ and ]]), this function converts it
 * into a safe page name by removing white space at each end and restricting to
 * max 200 characters. Also splits out the title (if provided).
 *
 * @param string $wikilink HTML code between [[ and ]]
 * @return object Object with parameters ->page (page name as PHP UTF-8
 *   string), ->title (link title as HTML; either an explicit title if specified
 *   or the start page string or the page name as html), ->rawpage (page name
 *   as HTML including possible entities, tags), and ->rawtitle (link title if
 *   specified as HTML including possible entities, tags; null if not specified)
 */
function ouwiki_get_wiki_link_details($wikilink)
{
    // Split out title if present (note: because | is lower-ascii it is safe
    // to use byte functions rather than UTF-8 ones)
    $rawtitle = null;
    $bar = strpos($wikilink, '|');
    if ($bar !== false) {
        $rawtitle = trim(substr($wikilink, $bar + 1));
        $wikilink = substr($wikilink, 0, $bar);
    }
    // Remove whitespace at either end
    $wikilink = trim($wikilink);
    $rawpage = $wikilink;
    // Remove html tags
    $wikilink = html_entity_decode(preg_replace('/<.*?>/', '', $wikilink), ENT_QUOTES, 'UTF-8');
    // Trim to 200 characters or less (note: because we don't want to cut it off
    // in the middle of a character, we use proper UTF-8 functions)
    if (core_text::strlen($wikilink) > 200) {
        $wikilink = core_text::substr($wikilink, 0, 200);
        $space = core_text::strrpos($wikilink, ' ');
        if ($space > 150) {
            $wikilink = core_text::substr($wikilink, 0, $space);
        }
    }
    // Remove non-breaking spaces
    $wikilink = str_replace(html_entity_decode('&nbsp;', ENT_QUOTES, 'UTF-8'), ' ', $wikilink);
    // What will the title be of this link?
    if ($rawtitle) {
        $title = $rawtitle;
    } else {
        if ($wikilink === '') {
            $title = get_string('startpage', 'ouwiki');
        } else {
            $title = $rawpage;
        }
    }
    // Return object with both pieces of information
    return (object) array('page' => $wikilink, 'title' => $title, 'rawtitle' => $rawtitle, 'rawpage' => $rawpage);
}
示例#3
0
 /**
  * Generate the name of the mod instance from the name of the file
  * (remove the extension and convert underscore => space
  *
  * @param string $filename the filename of the uploaded file
  * @return string the display name to use
  */
 protected function display_name_from_file($filename)
 {
     $pos = core_text::strrpos($filename, '.');
     if ($pos) {
         // Want to skip if $pos === 0 OR $pos === false.
         $filename = core_text::substr($filename, 0, $pos);
     }
     return str_replace('_', ' ', $filename);
 }
示例#4
0
/**
 * Filter out "." and ".." segments from a URL's path and return
 * the result.
 *
 * This function implements the "remove_dot_segments" algorithm from
 * the RFC3986 specification for URLs.
 *
 * This function supports multi-byte characters with the UTF-8 encoding,
 * per the URL specification.
 *
 * Parameters:
 * 	path	the path to filter
 *
 * Return values:
 * 	The filtered path with "." and ".." removed.
 */
function url_remove_dot_segments($path)
{
    // multi-byte character explode
    $inSegs = preg_split('!/!u', $path);
    $outSegs = array();
    foreach ($inSegs as $seg) {
        if ($seg == '' || $seg == '.') {
            continue;
        }
        if ($seg == '..') {
            array_pop($outSegs);
        } else {
            array_push($outSegs, $seg);
        }
    }
    $outPath = implode('/', $outSegs);
    if ($path[0] == '/') {
        $outPath = '/' . $outPath;
    }
    // Compare last multi-byte character against '/'.
    if ($outPath != '/' && core_text::strlen($path) - 1 == core_text::strrpos($path, '/', 'UTF-8')) {
        $outPath .= '/';
    }
    return $outPath;
}