Share a PHP website SEO and filtering class for ease of use-PHP source code

Source: Internet
Author: User
Tags php website
Some functions are used for SEO on website pages. For example, functions such as good URL generation, user name filtering, email detection, string truncation, full-width replacement with half-width, and html deletion are used in SEO for website pages.
For example: good URL generation, user name filtering, email detection, string truncation, full-width replacement with half-width, html deletion, etc.

1. [PHP] code

 'A', '/é | percent/' => 'e', '/percent | í | percent/' => 'I ', '/Upper | ó | lower | ø/' => 'O', '/lower | ú| lower | lower/' => 'u ', '/c | response/' => 'C', '/n | response/' => 'N', '/response/' => 'L ', '/keys/' => 'y', '/keys/' => 'T', '/keys/' => 'Z ', '/â/' =>'s ','/queues/'=>' AE ','/ö/ '=> 'Oe ', '/u/' => 'ue ','/ä/ '=>' AE ','/u/'=> 'ue ', '/ö/' => 'Oe ','/Lower/'=> 'SS',' // '=> '',' // '=> '', '/~ | · |! | @ | # | ¥ | % |... | & | × | (|) |-| \ + | = | '| [|' |] |, |: |; | "|" | \ '|, | "|. |? | \/|-| _ |: | √ | <| ° |, |> |-|★| │ | ‖ | Forward/'=> '','/[^ \ w \ s \ x80-\ xff]/'=> '', '/\ s +/' => $ replacement); $ string = trim ($ string); $ string = preg_replace (array_keys ($ map), array_values ($ map ), $ string); $ string = preg_replace ('/\ s +/', $ replacement, strtolower ($ string); $ string = trim ($ string, $ replacement ); return $ string;} // determines the email public function isEmail ($ mailAddr) {return strlen ($ mailAddr)> 6 & preg_match ("/^ [\ w \-\.] + @ [\ w \ -] + (\. \ W +) + $/", $ mailAddr);} // filter the public function CheckUser ($ string, $ replacement = '_') {$ map = array ('/à | á | upper | â | ä/' => 'A ', '/è | é | percent/' => 'e', '/percent | í | percent/' => 'I ', '/Upper | ó | lower | ø/' => 'O', '/lower | ú| lower | lower/' => 'u ', '/c | response/' => 'C', '/n | response/' => 'N', '/response/' => 'L ', '/keys/' => 'y', '/keys/' => 'T', '/keys/' => 'Z ', '/â/' =>'s ','/queues/'=>' AE ','/ö/ '=> 'Oe ', '/u/' => 'ue ','/ä/ '=>' AE ','/u/'=> 'ue ', '/ö/' => 'Oe ', '/Outputs/' => 'SS', '//' => '', '//' => '','/~ | · |! | @ | # | ¥ | % |... | & | × | (|) |-| \ + | = | '| [|' |] |, |: |; | "|" | \ '|, | "|. |? | \/|-| _ |: | √ | <| ° |, |> |-|★| │ | ‖ | Forward/'=> '','/[^ \ w \ s \ x80-\ xff]/'=> '', '/\ s +/' => $ replacement); $ string = trim ($ string); $ string = preg_replace (array_keys ($ map), array_values ($ map ), $ string); $ string = preg_replace ('/\ s +/', $ replacement, strtolower ($ string); $ string = trim ($ string, $ replacement ); return $ string;} public function mktitle ($ string, $ replacement = '_') {$ map = array ('/à | á | upper | â | ä/' => 'A', '/è | é | lower/' => 'E', '/region | í | region/' => 'I', '/region | ó | percent | ø/' => 'O ', '/province | ú| province | province/' => 'u', '/ç| province/' => 'C ', '/n | random/' => 'n' ','/random/'=> 'L','/random/'=> 'y ', '/outputs/' => 'T','/outputs/'=> 'Z','/comment/'=>' s ', '/outputs/' => 'AE', '/ö/' => 'Oe ','/u/'=> 'ue ', '/ä/' => 'AE', '/u/' => 'ue ','/ö/ '=> 'Oe ', '/outputs/' => 'SS', '//' => '', '//' => '','/~ | · |! | @ | # | ¥ | % |... | & | × | (|) |-| \ + | = | '| [|' |] |, |: |; | "|" | \ '|, | "|. |? | \/|-| _ |: | √ | <| ° |, |> |-|★| │ | ‖ | Percent/'=> '','/[^ \ w \ s ^. \ x80-\ xff]/'=> '','/\ s +/'=> $ replacement); $ string = trim ($ string ); $ string = preg_replace (array_keys ($ map), array_values ($ map), $ string); $ string = preg_replace ('/\ s +/', $ replacement, strtolower ($ string); $ string = trim ($ string, $ replacement); return $ string;} // replace the width with the width of public function Qj2bj ($ string) {$ qj2bj = array ('1' => '1', '2' => '2', '3' => '3', '4' => '4' ', '5' => '5', '6' => '6', '7' => '7', '8' => '8 ', '9' => '9', '0' => '0', 'a' => 'A', 'B' => 'B ', 'C' => 'C', 'D' => 'D', 'E' => 'e', 'F' => 'F ', 'G' => 'G', 'H' => 'h', 'I' => 'I', 'J' => 'J ', 'K' => 'K', 'L' => 'L', 'M' => 'm', 'n' => 'N ', 'o' => 'O', 'P' => 'P', 'q' => 'Q', 'R' => 'R ','s '=>'s', 'T' => 'T', 'u' => 'u', 'V' => 'V ', 'W' => 'W', 'x' => 'X', 'y' => 'y', 'Z' => 'Z ', 'A' => 'A ',' B '=>' B ', 'c' => 'C', 'D' => 'D', 'E' => 'e ', 'F' => 'F', 'G' => 'G', 'H' => 'h', 'I' => 'I ', 'j' => 'J', 'K' => 'K', 'L' => 'L', 'M' =>'m ', 'n' => 'N', 'O' => 'O', 'P' => 'P', 'q' => 'Q ', 'R' => 'R', 's' =>'s ', 'T' => 'T', 'u' => 'u ', 'V' => 'V', 'W' => 'W', 'x' => 'X', 'y' => 'y ', 'Z' => 'Z', ''=>''); return strtr ($ string, $ qj2bj);} public function DelNbsp ($ string) {$ map = array ('//' => '','/\? /'=> '',' // '=>'',); $ String = preg_replace (array_keys ($ map), array_values ($ map), $ string ); $ string = preg_replace ('/\ s +/', ', $ string); return $ string;} public function getZipcode ($ str) {$ pattern = "/[0-9] {1} (\ d +) {4, 5}/"; preg_match_all ($ pattern, $ str, $ zipcodeArr ); if (empty ($ zipcodeArr [0]) {return '';} else {return $ zipcodeArr [0] [0] ;}} public function checkZipcode ($ str) {$ strwidth = strlen ($ Str); $ zipcode = $ str; switch ($ strwidth) {case 4: $ zipcode = "00 ". $ str; break; case 5: $ zipcode = "0 ". $ str; break; case 6: $ zipcode = $ str; break; default: $ zipcode = ""; break;} return $ zipcode;} public function getEmail ($ str) {$ pattern = "/\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) */I "; preg_match_all ($ pattern, $ str, $ emailArr); if (empty ($ emailArr [0]) {return '';} else {$ email_str = implode (",", $ emailArr [0 ]); Return $ email_str;} public function getUrl ($ str) {$ pattern = "/(http: \/| https: \/| ftp: \/\/)? ([\ W :\/\.\? = &-_] +)/Is "; preg_match_all ($ pattern, $ str, $ urlArr); if (empty ($ urlArr [0]) {return '';} else {$ url_str = implode (",", $ urlArr [0]); $ url_str = preg_replace ('/(http: \/| https: \/| ftp: \/)/', '', $ url_str); return $ url_str ;}} public function match_links ($ document) {$ match = array (); preg_match_all ("'<\ s * a \ s. *? Href \ s * = \ s * ([\ "\ '])? (? (1 )(.*?) \ 1 | ([^ \ s \>] +) [^>] *>? (.*?) 'Isx ", $ document, $ links); while (list ($ key, $ val) = each ($ links [2]) {if (! Empty ($ val) $ match ['link'] [] = $ val;} while (list ($ key, $ val) = each ($ links [3]) {if (! Empty ($ val) $ match ['link'] [] = $ val;} while (list ($ key, $ val) = each ($ links [4]) {if (! Empty ($ val) $ match ['content'] [] = $ val;} while (list ($ key, $ val) = each ($ links [0]) {if (! Empty ($ val) $ match ['all'] [] = $ val;} return $ match;} public function DelNoStr ($ string) {$ map = array ('/à | á | upper | â | ä/' => 'A ', '/è | é | percent/' => 'e', '/percent | í | percent/' => 'I ', '/Upper | ó | lower | ø/' => 'O', '/lower | ú| lower | lower/' => 'u ', '/c | response/' => 'C', '/n | response/' => 'N', '/response/' => 'L ', '/keys/' => 'y', '/keys/' => 'T', '/keys/' => 'Z ', '/â/' =>'s ','/queues/'=>' AE ','/ö/ '=> 'Oe ', '/u/' => 'ue ','/ä/ '=>' AE ','/u/'=> 'ue ', '/ö/' => 'Oe ',' /Outputs/'=> 'SS',' // '=> '',' // '=> '','/~ | · |! | @ | # | ¥ | % |... | & | × | (|) |-| \ + | = | '| [|' |] |, |: |; | "|" | '|, | "|. |? | \/|-| _ | '|: | √ | <| ° |, | Bytes/' => '', '/[^ \ w \ s \ x80-\ xff]/' => '', // '/\ s +/' => $ replacement ); $ string = trim ($ string); $ string = preg_replace (array_keys ($ map), array_values ($ map), $ string ); $ string = preg_replace ('/\ s +/', '', $ string); $ string = trim ($ string,''); return $ string ;} public function UrlToStr ($ string, $ find = '/-/') {$ string = preg_replace ($ find, "", strtolower ($ string )); $ string = trim ($ string); return $ string;} public function GetPyLetter ($ str) {return substr (pinyin ($ str, $ ucfirst = true), 0, 1 );} public function GetPinYin ($ str) {return strtolower (pinyin ($ str, $ ucfirst = true);} public function DelCode ($ str) {$ search = array ("'
 
  
] *?>. *? Script 'Si ", // strip out javascript" '<[\/\!] *? [^ <>] *?> 'Si ", // strip out html tags" '([\ r \ n]) [\ s] + '", // strip out white space "'& (quot | #34 | #034 | # x22);' I ", // replace html entities "'& (amp | #38 | #038 | # x26);' I ", // added hexadecimal values "'& (lt | #60 | #060 | # x3c);' I ", "'& (gt | #62 | #062 | # x3e);' I", "'& (nbsp | #160 | # xa0);' I ", "'& (iexcl | #161);' I", "'& (cent | #162);' I", "'& (pound | #163 ); 'I ","' & (copy | #169); 'I ","' & (reg | #174); 'I ", "'& (deg | #176);' I", "'& (#39 | #039 | # x27 ); '","' & (Euro | #8364); 'I ", // europe"' & a (uml | UML );'", // german "'& o (uml | UML);'", "'& u (uml | UML);'", "'& A (uml | UML ); '","' & O (uml | UML); '","' & U (uml | UML); '"," 'region' I ",); $ replace = array ("", "", "\ 1", "\" "," & "," <","> ","", chr (161), chr (162), chr (163), chr (169), chr (174), chr (176), chr (39), chr (128 ), "? ","? ","? ","? ","? ","? ","? ",); $ Str = preg_replace ($ search, $ replace, $ str); return trim ($ str);} public function CutStr ($ sourcestr, $ cutlength) {$ returnstr = ''; $ I = 0; $ n = 0; $ sourcestr = rtrim (SeoTools: DelCode ($ sourcestr )); $ str_length = strlen ($ sourcestr); // Number of bytes of the string while ($ n <$ cutlength) and ($ I <= $ str_length )) {$ temp_str = substr ($ sourcestr, $ I, 1); $ ascnum = Ord ($ temp_str ); // Obtain the ascii code of the $ I character in the string if ($ ascnum> = 224) // if the ASCII bit height is 224, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 3); // count three consecutive characters as a single character according to the UTF-8 encoding specification $ I = $ I + 3; // The actual bytes are calculated as 3 $ n ++; // string length sensor 1} elseif ($ ascnum> = 192) // if the ASCII bit height is 192, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 2); // Count 2 consecutive characters as a single character according to the UTF-8 encoding specification $ I = $ I + 2; // The actual Byte is calculated as 2 $ n ++; // string length gauge 1} elseif ($ ascnum> = 65 & $ ascnum <= 90) // if it is a capital letter, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 1); $ I = $ I + 1; // The actual number of bytes is still 1 $ n ++; // but consider the overall appearance. uppercase letters are counted as a high character} else // In other cases, including lowercase letters and halfwidth punctuation marks, {$ returnstr = $ returnstr. substr ($ sourcestr, $ I, 1); $ I = $ I + 1; // The actual number of bytes is 1 $ n = $ n + 0.5; // lower-case letters, Halfwidth punctuation marks, and half-width upper-limit characters ...}} if ($ str_length> $ I) {$ returnstr = $ returnstr. ". "; // when the length is exceeded, add the ellipsis} return htmlspecialchars ($ returnstr);} public function CreateTagLink ($ tags_str) {$ tags_array = explode (",", $ tags_str); $ link_tpl = ""; foreach ($ tags_array as $ v) {$ tag = trim ($ v); $ alias = SeoTools: friendlyURL ($ tag ); // $ link_tpl. = ''. $ tag. ''; $ link_tpl. = $ tag. '';} return $ link_tpl;}/*** conversion class ** @ author Lukin
  
   
* @ Date */class PinYin {// code table private $ fp = null; private $ dat = 'pinyin. dat '; public function _ construct () {$ this-> dat = dirname (_ FILE __). '/'. $ this-> dat; if (is_file ($ this-> dat) {$ this-> fp = fopen ($ this-> dat, 'RB ');}} /*** convert Pinyin ** @ param string $ str Chinese character * @ param bool $ upper letter of ucfirst * @ param bool $ polyphony ignore multiple pronunciations * @ return string */public function encode ($ str, $ ucfirst = true, $ Polyphony = true) {$ ret = ''; $ len = mb_strlen ($ str, 'utf-8'); for ($ I = 0; $ I <$ len; $ I ++) {$ py = $ this-> pinyin (mb_substr ($ str, $ I, 1, 'utf-8 ')); if ($ ucfirst & strpos ($ py ,',')! = False) {$ pys = explode (',', $ py); $ ret. = implode (',', array_map ('ucfirst ', ($ polyphony? Array_slice ($ pys, 0, 1): $ pys);} else {$ ret. = $ ucfirst? Ucfirst ($ py): $ py;} return $ ret ;} /*** convert Chinese characters to decimal characters ** @ param string $ word * @ return number */private function char2dec ($ word) {$ bins = ''; $ chars = str_split ($ word); foreach ($ chars as $ char) $ bins. = decbin (ord ($ char); $ bins = preg_replace ('/^. {4 }(. {4 }). {2 }(. {6 }). {2 }(. {6}) $/',' $1 $2 $ 3', $ bins); return bindec ($ bins );} /*** convert a single character to pinyin ** @ param string $ char Chinese character * @ return string */public function Pinyin ($ char) {if (strlen ($ char) = 3 & $ this-> fp) {$ offset = $ this-> char2dec ($ char ); // Determine the off value if ($ offset >=0) {fseek ($ this-> fp, ($ offset-19968) <4, SEEK_SET ); return trim (fread ($ this-> fp, 16) ;}return $ char;} public function _ destruct () {if ($ this-> fp) {fclose ($ this-> fp) ;}}/*** obtain the instance ** @ return $ pinyin */function & _ pinyin_get_object () {static $ pinyin; if (is_null ($ pinyin) $ Pinyin = new PinYin (); return $ pinyin;} if (! Function_exists ('Pinin ')): /*** obtain pinyin ** @ param string $ str * @ param bool $ upper letter of ucfirst * @ return string */function pinyin ($ str, $ ucfirst = true) {$ py = _ pinyin_get_object (); return $ py-> encode ($ str, $ ucfirst);} endif;
  
 

The above is to share a PHP website SEO and filtering class for ease of use. For more information, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.