[{"data":1,"prerenderedAt":237},["ShallowReactive",2],{"content-query-77ZJQgAYp0":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"category":5,"createdAt":10,"updatedAt":10,"body":11,"_type":231,"_id":232,"_source":233,"_file":234,"_stem":235,"_extension":236},"/article/coding/deal-with-non-breaking-space","coding",false,"","不換行空格處理","最近爬資料時遇到一個很奇怪的問題，拿到的資料字串前後都有空格，無法直接用 PHP 的 trim() 或是 str_replace() 拿掉空格，仔細排查了一下原來這是一個叫做不換行空格 Non-breaking space 的東西，知道這是啥東西之後就好辦了，以下兩種處理方式都可以:","2023-04-03",{"type":12,"children":13,"toc":229},"root",[14,39,55,138,153,218,223],{"type":15,"tag":16,"props":17,"children":18},"element","p",{},[19,22,29,31,37],{"type":20,"value":21},"text","最近爬資料時遇到一個很奇怪的問題，拿到的資料字串前後都有空格，無法直接用 PHP 的 ",{"type":15,"tag":23,"props":24,"children":26},"code",{"className":25},[],[27],{"type":20,"value":28},"trim()",{"type":20,"value":30}," 或是 ",{"type":15,"tag":23,"props":32,"children":34},{"className":33},[],[35],{"type":20,"value":36},"str_replace()",{"type":20,"value":38}," 拿掉空格，仔細排查了一下原來這是一個叫做不換行空格 Non-breaking space 的東西，知道這是啥東西之後就好辦了，以下兩種處理方式都可以:",{"type":15,"tag":40,"props":41,"children":42},"ol",{},[43],{"type":15,"tag":44,"props":45,"children":46},"li",{},[47,49],{"type":20,"value":48},"UTF-8 編碼 ",{"type":15,"tag":23,"props":50,"children":52},{"className":51},[],[53],{"type":20,"value":54},"C2 A0",{"type":15,"tag":56,"props":57,"children":61},"pre",{"className":58,"code":59,"language":60,"meta":7,"style":7},"language-php shiki shiki-themes material-theme-palenight","preg_replace(\"/\\xc2\\xa0/\", \"\", $firstChild->data);\n","php",[62],{"type":15,"tag":23,"props":63,"children":64},{"__ignoreMap":7},[65],{"type":15,"tag":66,"props":67,"children":70},"span",{"class":68,"line":69},"line",1,[71,77,83,88,94,99,104,109,113,118,123,128,133],{"type":15,"tag":66,"props":72,"children":74},{"style":73},"--shiki-default:#82AAFF",[75],{"type":20,"value":76},"preg_replace",{"type":15,"tag":66,"props":78,"children":80},{"style":79},"--shiki-default:#89DDFF",[81],{"type":20,"value":82},"(",{"type":15,"tag":66,"props":84,"children":85},{"style":79},[86],{"type":20,"value":87},"\"/",{"type":15,"tag":66,"props":89,"children":91},{"style":90},"--shiki-default:#BABED8",[92],{"type":20,"value":93},"\\xc2\\xa0",{"type":15,"tag":66,"props":95,"children":96},{"style":79},[97],{"type":20,"value":98},"/\"",{"type":15,"tag":66,"props":100,"children":101},{"style":79},[102],{"type":20,"value":103},",",{"type":15,"tag":66,"props":105,"children":106},{"style":79},[107],{"type":20,"value":108}," \"\"",{"type":15,"tag":66,"props":110,"children":111},{"style":79},[112],{"type":20,"value":103},{"type":15,"tag":66,"props":114,"children":115},{"style":79},[116],{"type":20,"value":117}," $",{"type":15,"tag":66,"props":119,"children":120},{"style":90},[121],{"type":20,"value":122},"firstChild",{"type":15,"tag":66,"props":124,"children":125},{"style":79},[126],{"type":20,"value":127},"->",{"type":15,"tag":66,"props":129,"children":130},{"style":90},[131],{"type":20,"value":132},"data",{"type":15,"tag":66,"props":134,"children":135},{"style":79},[136],{"type":20,"value":137},");\n",{"type":15,"tag":40,"props":139,"children":141},{"start":140},2,[142],{"type":15,"tag":44,"props":143,"children":144},{},[145,147],{"type":20,"value":146},"Unicode 編碼 ",{"type":15,"tag":23,"props":148,"children":150},{"className":149},[],[151],{"type":20,"value":152},"U+00A0",{"type":15,"tag":56,"props":154,"children":156},{"className":58,"code":155,"language":60,"meta":7,"style":7},"preg_replace(\"/\\x{00a0}/u\", \"\", $firstChild->data);\n",[157],{"type":15,"tag":23,"props":158,"children":159},{"__ignoreMap":7},[160],{"type":15,"tag":66,"props":161,"children":162},{"class":68,"line":69},[163,167,171,175,181,186,190,194,198,202,206,210,214],{"type":15,"tag":66,"props":164,"children":165},{"style":73},[166],{"type":20,"value":76},{"type":15,"tag":66,"props":168,"children":169},{"style":79},[170],{"type":20,"value":82},{"type":15,"tag":66,"props":172,"children":173},{"style":79},[174],{"type":20,"value":87},{"type":15,"tag":66,"props":176,"children":178},{"style":177},"--shiki-default:#C3E88D",[179],{"type":20,"value":180},"\\x{00a0}",{"type":15,"tag":66,"props":182,"children":183},{"style":79},[184],{"type":20,"value":185},"/u\"",{"type":15,"tag":66,"props":187,"children":188},{"style":79},[189],{"type":20,"value":103},{"type":15,"tag":66,"props":191,"children":192},{"style":79},[193],{"type":20,"value":108},{"type":15,"tag":66,"props":195,"children":196},{"style":79},[197],{"type":20,"value":103},{"type":15,"tag":66,"props":199,"children":200},{"style":79},[201],{"type":20,"value":117},{"type":15,"tag":66,"props":203,"children":204},{"style":90},[205],{"type":20,"value":122},{"type":15,"tag":66,"props":207,"children":208},{"style":79},[209],{"type":20,"value":127},{"type":15,"tag":66,"props":211,"children":212},{"style":90},[213],{"type":20,"value":132},{"type":15,"tag":66,"props":215,"children":216},{"style":79},[217],{"type":20,"value":137},{"type":15,"tag":16,"props":219,"children":220},{},[221],{"type":20,"value":222},"這樣就移除掉空白了！",{"type":15,"tag":224,"props":225,"children":226},"style",{},[227],{"type":20,"value":228},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":140,"depth":140,"links":230},[],"markdown","content:article:coding:deal-with-non-breaking-space.md","content","article/coding/deal-with-non-breaking-space.md","article/coding/deal-with-non-breaking-space","md",1766375576074]