{"action":"created","issue":{"url":"https://api.github.com/repos/top-think/framework/issues/47","repository_url":"https://api.github.com/repos/top-think/framework","labels_url":"https://api.github.com/repos/top-think/framework/issues/47/labels{/name}","comments_url":"https://api.github.com/repos/top-think/framework/issues/47/comments","events_url":"https://api.github.com/repos/top-think/framework/issues/47/events","html_url":"https://github.com/top-think/framework/issues/47","id":151778865,"number":47,"title":"strtr is a better way to replace strings in Line 122","user":{"login":"stenote","id":1109648,"avatar_url":"https://avatars.githubusercontent.com/u/1109648?v=3","gravatar_id":"","url":"https://api.github.com/users/stenote","html_url":"https://github.com/stenote","followers_url":"https://api.github.com/users/stenote/followers","following_url":"https://api.github.com/users/stenote/following{/other_user}","gists_url":"https://api.github.com/users/stenote/gists{/gist_id}","starred_url":"https://api.github.com/users/stenote/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stenote/subscriptions","organizations_url":"https://api.github.com/users/stenote/orgs","repos_url":"https://api.github.com/users/stenote/repos","events_url":"https://api.github.com/users/stenote/events{/privacy}","received_events_url":"https://api.github.com/users/stenote/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-29T01:45:52Z","updated_at":"2016-04-29T02:21:23Z","closed_at":null,"body":"https://github.com/top-think/framework/blob/master/library/think/View.php#L122\r\n\r\n修改为如下，更清晰，效率更高\r\n\r\n```\r\n$content = strtr($content, $this->replace);\r\n```\r\n\r\nref:\r\n\r\n* http://stackoverflow.com/questions/8177296/when-to-use-strtr-vs-str-replace\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/top-think/framework/issues/comments/215612691","html_url":"https://github.com/top-think/framework/issues/47#issuecomment-215612691","issue_url":"https://api.github.com/repos/top-think/framework/issues/47","id":215612691,"user":{"login":"hugtale","id":7259708,"avatar_url":"https://avatars.githubusercontent.com/u/7259708?v=3","gravatar_id":"","url":"https://api.github.com/users/hugtale","html_url":"https://github.com/hugtale","followers_url":"https://api.github.com/users/hugtale/followers","following_url":"https://api.github.com/users/hugtale/following{/other_user}","gists_url":"https://api.github.com/users/hugtale/gists{/gist_id}","starred_url":"https://api.github.com/users/hugtale/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hugtale/subscriptions","organizations_url":"https://api.github.com/users/hugtale/orgs","repos_url":"https://api.github.com/users/hugtale/repos","events_url":"https://api.github.com/users/hugtale/events{/privacy}","received_events_url":"https://api.github.com/users/hugtale/received_events","type":"User","site_admin":false},"created_at":"2016-04-29T02:21:23Z","updated_at":"2016-04-29T02:21:23Z","body":"First difference:\r\n=\r\n\r\nAn interesting example of a different behaviour between strtr and str_replace is in the comments section of the PHP Manual:\r\n\r\n~~~\r\n<?php\r\n$arrFrom = array(\"1\",\"2\",\"3\",\"B\");\r\n$arrTo = array(\"A\",\"B\",\"C\",\"D\");\r\n$word = \"ZBB2\";\r\necho str_replace($arrFrom, $arrTo, $word);\r\n?>\r\n~~~\r\nI would expect as result: \"ZDDB\"\r\nHowever, this return: \"ZDDD\" (Because B = D according to our array)\r\nTo make this work, use \"strtr\" instead:\r\n\r\n~~~\r\n<?php\r\n$arr = array(\"1\" => \"A\",\"2\" => \"B\",\"3\" => \"C\",\"B\" => \"D\");\r\n$word = \"ZBB2\";\r\necho strtr($word,$arr);\r\n?>\r\n~~~\r\nThis returns: \"ZDDB\"\r\nThis means that str_replace is a more global approach to replacements, while strtr simply translates the chars one by one.\r\n\r\nAnother difference:\r\n=\r\n\r\nGiven the following code (taken from PHP String Replacement Speed Comparison):\r\n\r\n~~~\r\n<?php\r\n$text = \"PHP: Hypertext Preprocessor\";\r\n\r\n$text_strtr = strtr($text\r\n    , array(\"PHP\" => \"PHP: Hypertext Preprocessor\"\r\n        , \"PHP: Hypertext Preprocessor\" => \"PHP\"));\r\n$text_str_replace = str_replace(array(\"PHP\", \"PHP: Hypertext Preprocessor\")\r\n    , array(\"PHP: Hypertext Preprocessor\", \"PHP\")\r\n    , $text);\r\nvar_dump($text_strtr);\r\nvar_dump($text_str_replace);\r\n?>\r\n~~~\r\nThe resulting lines of text will be:\r\n\r\n~~~\r\nstring(3) \"PHP\"\r\nstring(27) \"PHP: Hypertext Preprocessor\"\r\n~~~"}}