Process image span
@return string
@param $str string
554316
protected function _img($str) {
		return preg_replace_callback(
			'/!(?:\[(.+?)\])?\h*\(<?(.*?)>?(?:\h*"(.*?)"\h*)?\)/',
			function($expr) {
				return '<img src="'.$expr[2].'"'.
					(empty($expr[1])?
						'':
						(' alt="'.$this->esc($expr[1]).'"')).
					(empty($expr[3])?
						'':
						(' title="'.$this->esc($expr[3]).'"')).' />';
			},
			$str
		);
	}
285666
private function toCamelCase($str)
    {
        // Split string in words.
        $words = preg_split('/[-_]/', strtolower($str)); //explode('_', strtolower($str));

        $return = '';
        foreach ($words as $word) {
            $return .= ucfirst(trim($word));
        }

        return $return;
    }
110946
public function parse($str)
	{
		if ($this->isQuoteHtml() === true)
		{
			$str = htmlspecialchars($str, ENT_QUOTES);
		}

		foreach ($this->getBbCodes() as $key => $val)
		{
			$str = preg_replace($key, $val, $str);
		}

		return $str;
	}
915957
private function _do($str)
    {
        $str = (string) $str;
        $strInt = (int) $str;
        $strFloat = (float) $str;
        if (
            !$str
            ||
            (string) $strInt === $str
            ||
            (string) $strFloat === $str
        ) {

            // no xss found
            if ($this->_xss_found !== true) {
                $this->_xss_found = false;
            }

            return $str;
        }

        // remove the BOM from UTF-8 / UTF-16 / UTF-32 strings
        $str = UTF8::remove_bom($str);

        // replace the diamond question mark (�) and invalid-UTF8 chars
        $str = UTF8::replace_diamond_question_mark($str, '');

        // replace invisible characters with one single space
        $str = UTF8::remove_invisible_characters($str, true, ' ');

        // normalize the whitespace
        $str = UTF8::normalize_whitespace($str);

        // decode UTF-7 characters
        $str = $this->_repack_utf7($str);

        // decode the string
        $str = $this->_decode_string($str);

        // remove all >= 4-Byte chars if needed
        if ($this->_stripe_4byte_chars) {
            $str = (string) \preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $str);
        }

        // backup the string (for later comparision)
        $str_backup = $str;

        // remove strings that are never allowed
        $str = $this->_do_never_allowed($str);

        // corrects words before the browser will do it
        $str = $this->_compact_exploded_javascript($str);

        // remove disallowed javascript calls in links, images etc.
        $str = $this->_remove_disallowed_javascript($str);

        // remove evil attributes such as style, onclick and xmlns
        $str = $this->_remove_evil_attributes($str);

        // sanitize naughty JavaScript elements
        $str = $this->_sanitize_naughty_javascript($str);

        // sanitize naughty HTML elements
        $str = $this->_sanitize_naughty_html($str);

        // final clean up
        //
        // -> This adds a bit of extra precaution in case something got through the above filters.
        $str = $this->_do_never_allowed_afterwards($str);

        // check for xss
        if ($this->_xss_found !== true) {
            $this->_xss_found = !($str_backup === $str);
        }

        return $str;
    }
138680
public function toString(string $str):string {
		$str = strtoupper($str);

		// csSave actually has to be able to consider extra characters
		if($this->charset === $this::CROCKFORD){
			$str = str_replace('O', '0', $str);
			$str = str_replace(['I', 'L'], '1', $str);
		}

		return $this->bin2str($this->toBin($str));
	}
568000
public static function cleanForOrder(string $str): string
  {
    $str = self::removeAccents($str);
    $str = preg_replace("/^[^a-z0-9 ]/i", "", $str);
    $str = trim($str);
    return $str;
  }
1324055
private function getDiscreteText($str) {
        $arr = array();
        for ( $i = 0; $i < strlen($str); $i++ ) {
            if ( $str[$i] == "\n" ) continue;
            if ( ord($str[$i]) < 127 ) {
                array_push($arr, $str[$i]);
                continue;
            }
            if ( ord($str[$i]) > 127 &&
                ord($str[$i+1]) > 127 &&
                ord($str[$i+2]) > 127 ) {
                array_push($arr, substr($str, $i, 3));
                $i += 2;
            }
        }
        return $arr;
    }
950204
protected function _text($str) {
		$tmp='';
		while ($str!=$tmp)
			$str=preg_replace_callback(
				'/(?<=\s|^)(?<!\\\\)([*_]{1,3})(.*?)(?!\\\\)\1(?=[\s[:punct:]]|$)/',
				function($expr) {
					switch (strlen($expr[1])) {
						case 1:
							return '<em>'.$expr[2].'</em>';
						case 2:
							return '<strong>'.$expr[2].'</strong>';
						case 3:
							return '<strong><em>'.$expr[2].'</em></strong>';
					}
				},
				preg_replace(
					'/(?<!\\\\)~~(.*?)(?!\\\\)~~(?=[\s[:punct:]]|$)/',
					'<del>\1</del>',
					$tmp=$str
				)
			);
		return $str;
	}
386470
public static function toSemiangle($str) {
		$arr = array(
				"０" => "0", "１" => "1", "２" => "2", "３" => "3", "４" => "4", "５" => "5",
				"６" => "6", "７" => "7", "８" => "8", "９" => "9", "Ａ" => "A", "Ｂ" => "B",
				"Ｃ" => "C", "Ｄ" => "D", "Ｅ" => "E", "Ｆ" => "F", "Ｇ" => "G", "Ｈ" => "H",
				"Ｉ" => "I", "Ｊ" => "J", "Ｋ" => "K", "Ｌ" => "L", "Ｍ" => "M", "Ｎ" => "N",
				"Ｏ" => "O", "Ｐ" => "P", "Ｑ" => "Q", "Ｒ" => "R", "Ｓ" => "S", "Ｔ" => "T",
				"Ｕ" => "U", "Ｖ" => "V", "Ｗ" => "W", "Ｘ" => "X", "Ｙ" => "Y", "Ｚ" => "Z",
				"ａ" => "a", "ｂ" => "b", "ｃ" => "c", "ｄ" => "d", "ｅ" => "e", "ｆ" => "f",
				"ｇ" => "g", "ｈ" => "h", "ｉ" => "i", "ｊ" => "j", "ｋ" => "k", "ｌ" => "l",
				"ｍ" => "m", "ｎ" => "n", "ｏ" => "o", "ｐ" => "p", "ｑ" => "q", "ｒ" => "r",
				"ｓ" => "s", "ｔ" => "t", "ｕ" => "u", "ｖ" => "v", "ｗ" => "w", "ｘ" => "x",
				"ｙ" => "y", "ｚ" => "z", "（" => "(", "）" => ")", "［" => "[", "］" => "]",
				"【" => "[", "】" => "]", "〖" => "[", "〗" => "]", "「" => "[", "」" => "]",
				"『" => "[", "』" => "]", "｛" => "{", "｝" => "}", "《" => "<", "》" => ">",
				"％" => "%", "＋" => "+", "—" => "-", "－" => "-", "～" => "-", "：" => ":",
				"。" => ".", "、" => ",", "，" => ".", "、" => ".", "；" => ",", "？" => "?",
				"！" => "!", "…" => "-", "‖" => "|", "　" => " ", "＇" => "`", "｀" => "`",
				"｜" => "|", "〃" => "\"", "＂" => "\"");
		return strtr($str, $arr);
	}
72984
public static function toString($str){
		$str = strtoupper($str);

		// csSave actually has to be able to consider extra characters
		if(self::$charset === Base32Characters::CROCKFORD){
			$str = str_replace('O', '0', $str);
			$str = str_replace(['I', 'L'], '1', $str);
		}

		return self::bin2str(self::toBin($str));
	}
493477
public static function getCharset($str) {
    if (empty($str)) {
      throw new Exception('Argument str cannot be empty');
    }
    $ord = self::unistr2ords($str);
    return self::getDominant($ord);
  }
132142
public function strToTopology( $str ) {
        $len = strlen( $str );
        $data = array(
            'str' => '',
            'u' => 0,
            'l' => 0,
            'd' => 0,
            's' => 0
        );
        
        //loop through each letter and build the topology
        //and count the letter types
        for( $i = 0; $i < $len; $i++ )  {
            if( ctype_upper( $str[$i]  ) ) {
                    $str[$i] = 'u';
                    $data['u']++;
            } elseif( ctype_lower( $str[$i] ) ) {
                    $str[$i] = 'l';
                    $data['l']++;
            } elseif( ctype_digit( $str[$i] ) ) {
                    $str[$i] = 'd';
                    $data['d']++;
            } else {
                    $str[$i] = 's';
                    $data['s']++;
            }
        }

        $data['str'] = $str;
        
        return $data;
    }
301766
private function parse($str)
    {
        $str = trim($str);
        $this->expression = $str;
        $strlen = strlen($str);
        $main_array = [];
        $count = 0;

        for ($i=0; $i<$strlen; $i++) {

            $str_i = $str[$i];
            $str_ip1 = (isset($str[$i+1])) ? $str[$i+1] : null;
            $str_ip2 = (isset($str[$i+2])) ? $str[$i+2] : null;
            $str_ip3 = (isset($str[$i+3])) ? $str[$i+3] : null;
            $str_ip4 = (isset($str[$i+4])) ? $str[$i+4] : null;
            $str_im1 = (isset($str[$i-1])) ? $str[$i-1] : null;
            $str_im2 = (isset($str[$i-2])) ? $str[$i-2] : null;

            // NaN
            if (stristr($str, 'NaN'))
                return NAN;
            // Spaces will be skipped
            if ($str_i == ' ') {
                $count++;
            // Number
            } elseif (is_numeric($str_i)) {
                $main_array[$count] = (isset($main_array[$count])) ?
                        $main_array[$count].$str_i : $str_i;
            // Constant pi
            } elseif ($str_im1.$str_i == 'pi') {
            } elseif (($str_i.$str_ip1 == 'pi')
            && (!$str_ip2 || strstr('+-*/^ ', $str_ip2))) {
                $count++;
                $main_array[$count] = M_PI;
            // Constant e
            } elseif (($str_i == 'e') && ($str_ip1 != 'x') && (!is_numeric($str_im1))
            && (!$str_ip1 || strstr('+-*/^ ', $str_ip1))) {
                $count++;
                $main_array[$count] = M_E;
            // Number in E notation
            } elseif ((strtolower($str_i) == 'e') && ($str_ip1 != 'x')
            && (is_numeric($str_im1))) {
                $main_array[$count] = $main_array[$count].'E';
            } elseif ((strstr('+-', $str_i)) && (strtolower($str_im1) == 'e')
            && (is_numeric($str_ip1)) && (is_numeric($str_im2))) {
                $main_array[$count] = $main_array[$count].$str_i;
            // Decimal point
            } elseif (($str_i == '.') && ((isset($str_im1) && is_numeric($str_im1)) 
            || (isset($str_ip1)) && is_numeric($str_ip1))) {
                $main_array[$count] = (isset($main_array[$count])) ? 
                        $main_array[$count].'.' : '.';
            // Function
            } elseif ((in_array($str_i.$str_ip1.$str_ip2, $this->valid_functions))
            || ($str_i.$str_ip1.$str_ip2.$str_ip3 == 'sqrt')) {
                if ($str_i.$str_ip1.$str_ip2.$str_ip3 == 'sqrt') {
                    if ($str_ip4 == ' ')
                        $this->calculateFunction('sqrt', $str, $strlen, $i);
                } else {
                    if ($str_ip3 == ' ')
                        $this->calculateFunction($str_i.$str_ip1.$str_ip2, $str, $strlen, $i);
                }
            // Variable
            } elseif (in_array($str_i, $this->valid_variables) && count($this->variables)) {
                if (array_key_exists($str_i, $this->variables)
                && is_numeric($this->variables[$str_i])) {
                    $main_array[$count] = (float) $this->variables[$str_i];
                    $count++;
                } else {
                    $this->correct = 0;
                    $this->error_type = 4;
                    break;
                }
            } else {
                // Operator
                $count++;
                if (strstr('+-*/^', $str_i)) {
                    if (!count($main_array) && $str_i == '+')
                        continue;
                    $main_array[$count] = $str_i;
                    $count++;
                } else {
                    // Constant Inf
                    if ($str_im2.$str_im1.$str_i == 'INF') {
                    } elseif ($str_im1.$str_i.$str_ip1 == 'INF') {
                    } elseif ($str_i.$str_ip1.$str_ip2 == 'INF') {
                        if ($str_im1 == '-' && !count($main_array)) {
                            $main_array[$count] = '-';
                            $count++;
                        }
                        $main_array[$count] = INF;
                        $count++;
                    } else {
                        // Nothing matches
                        $this->correct = 0;
                        if (!$this->validate())
                            $this->error_type = 1;
                        break;
                    }
                }
            }
        }

        if (!$this->correct)
            return 0;

        $main_array = array_values($main_array);

        if (isset($main_array[1])) {

            $main_array = $this->combine($main_array);
            $main_array = $this->calculate1($main_array);
            $main_array = $this->calculate2($main_array);
            $main_array = $this->calculate3($main_array);

            if (count($main_array) != 1)
                $this->correct = 0;
        }

        if (!isset($main_array[0]) || strstr('+-*/^', (string) $main_array[0])
        || substr_count((string) $main_array[0], '.') > 1) {
            $this->correct = 0;
            return 0;
        }

        return $this->return_($main_array[0]);
    }
55169
public static function getImageExtByBase64($str)
    {
        $strInfo = @unpack('c2chars', $str);
        if ($strInfo['chars1'] == '-1' && $strInfo['chars2'] == '-40') {
            $ext = 'jpg';
        } else if ($strInfo['chars1'] == '-119' && $strInfo['chars2'] == '80') {
            $ext = 'png';
        } else {
            $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']);
            switch ($typeCode) {
                case 7790 :
                    $ext = 'exe';
                    break;
                case 7784 :
                    $ext = 'midi';
                    break;
                case 8297 :
                    $ext = 'rar';
                    break;
                case 255216 :
                    $ext = 'jpg';
                    break;
                case 7173 :
                    $ext = 'gif';
                    break;
                case 6677 :
                    $ext = 'bmp';
                    break;
                case 13780:
                    $ext = 'png';
                    break;
                default   :
                    $ext = 'unknown' . $typeCode;
            }
        }
        return $ext;
    }
82468
public function widont($str) {
		$str = rtrim($str);
		$space = strrpos($str, ' ');

		if ($space !== false) {
			$str = substr($str, 0, $space) . '&nbsp;' . substr($str, $space + 1);
		}

		return $str;
	}
137769
private function _parse_css($str)
    {

        $str = trim($str);

        // Destroy comments and remove HTML comments
        $css = preg_replace(array(
            "'/\*.*?\*/'si",
            "/^<!--/",
            "/-->$/"
        ), "", $str);

        // FIXME: handle '{' within strings, e.g. [attr="string {}"]

        // Something more legible:
        $re =
            "/\s*                                   # Skip leading whitespace                             \n" .
            "( @([^\s{]+)\s*([^{;]*) (?:;|({)) )?   # Match @rules followed by ';' or '{'                 \n" .
            "(?(1)                                  # Only parse sub-sections if we're in an @rule...     \n" .
            "  (?(4)                                # ...and if there was a leading '{'                   \n" .
            "    \s*( (?:(?>[^{}]+) ({)?            # Parse rulesets and individual @page rules           \n" .
            "            (?(6) (?>[^}]*) }) \s*)+?                                                        \n" .
            "       )                                                                                     \n" .
            "   })                                  # Balancing '}'                                       \n" .
            "|                                      # Branch to match regular rules (not preceded by '@') \n" .
            "([^{]*{[^}]*}))                        # Parse normal rulesets                               \n" .
            "/xs";

        if (preg_match_all($re, $css, $matches, PREG_SET_ORDER) === false) {
            // An error occurred
            throw new Exception("Error parsing css file: preg_match_all() failed.");
        }

        // After matching, the array indicies are set as follows:
        //
        // [0] => complete text of match
        // [1] => contains '@import ...;' or '@media {' if applicable
        // [2] => text following @ for cases where [1] is set
        // [3] => media types or full text following '@import ...;'
        // [4] => '{', if present
        // [5] => rulesets within media rules
        // [6] => '{', within media rules
        // [7] => individual rules, outside of media rules
        //

        $media_query_regex = "/(?:((only|not)?\s*(" . implode("|", self::$VALID_MEDIA_TYPES) . "))|(\s*\(\s*((?:(min|max)-)?([\w\-]+))\s*(?:\:\s*(.*?)\s*)?\)))/isx";

        //Helpers::pre_r($matches);
        foreach ($matches as $match) {
            $match[2] = trim($match[2]);

            if ($match[2] !== "") {
                // Handle @rules
                switch ($match[2]) {

                    case "import":
                        $this->_parse_import($match[3]);
                        break;

                    case "media":
                        $acceptedmedia = self::$ACCEPTED_GENERIC_MEDIA_TYPES;
                        $acceptedmedia[] = $this->_dompdf->getOptions()->getDefaultMediaType();

                        $media_queries = preg_split("/\s*,\s*/", mb_strtolower(trim($match[3])));
                        foreach ($media_queries as $media_query) {
                            if (in_array($media_query, $acceptedmedia)) {
                                //if we have a media type match go ahead and parse the stylesheet
                                $this->_parse_sections($match[5]);
                                break;
                            } elseif (!in_array($media_query, self::$VALID_MEDIA_TYPES)) {
                                // otherwise conditionally parse the stylesheet assuming there are parseable media queries
                                if (preg_match_all($media_query_regex, $media_query, $media_query_matches, PREG_SET_ORDER) !== false) {
                                    $mq = array();
                                    foreach ($media_query_matches as $media_query_match) {
                                        if (empty($media_query_match[1]) === false) {
                                            $media_query_feature = strtolower($media_query_match[3]);
                                            $media_query_value = strtolower($media_query_match[2]);
                                            $mq[] = array($media_query_feature, $media_query_value);
                                        } else if (empty($media_query_match[4]) === false) {
                                            $media_query_feature = strtolower($media_query_match[5]);
                                            $media_query_value = (array_key_exists(8, $media_query_match) ? strtolower($media_query_match[8]) : null);
                                            $mq[] = array($media_query_feature, $media_query_value);
                                        }
                                    }
                                    $this->_parse_sections($match[5], $mq);
                                    break;
                                }
                            }
                        }
                        break;

                    case "page":
                        //This handles @page to be applied to page oriented media
                        //Note: This has a reduced syntax:
                        //@page { margin:1cm; color:blue; }
                        //Not a sequence of styles like a full.css, but only the properties
                        //of a single style, which is applied to the very first "root" frame before
                        //processing other styles of the frame.
                        //Working properties:
                        // margin (for margin around edge of paper)
                        // font-family (default font of pages)
                        // color (default text color of pages)
                        //Non working properties:
                        // border
                        // padding
                        // background-color
                        //Todo:Reason is unknown
                        //Other properties (like further font or border attributes) not tested.
                        //If a border or background color around each paper sheet is desired,
                        //assign it to the <body> tag, possibly only for the css of the correct media type.

                        // If the page has a name, skip the style.
                        $page_selector = trim($match[3]);

                        $key = null;
                        switch ($page_selector) {
                            case "":
                                $key = "base";
                                break;

                            case ":left":
                            case ":right":
                            case ":odd":
                            case ":even":
                            /** @noinspection PhpMissingBreakStatementInspection */
                            case ":first":
                                $key = $page_selector;

                            default:
                                continue;
                        }

                        // Store the style for later...
                        if (empty($this->_page_styles[$key])) {
                            $this->_page_styles[$key] = $this->_parse_properties($match[5]);
                        } else {
                            $this->_page_styles[$key]->merge($this->_parse_properties($match[5]));
                        }
                        break;

                    case "font-face":
                        $this->_parse_font_face($match[5]);
                        break;

                    default:
                        // ignore everything else
                        break;
                }

                continue;
            }

            if ($match[7] !== "") {
                $this->_parse_sections($match[7]);
            }

        }
    }
1161991
public function preProcessString($str)
    {
        $str = str_replace("<?php", "", $str);
        $str = str_replace("<?", "", $str);
        $str = str_replace("?>", "", $str);
        $str = str_replace("'", "\x22", $str);

        $str = trim($str);

        return $str;
    }