如何使用 PHP 转换 JSON 数据中的十六进制代码 [英] How to convert hex codes in JSON data using PHP

查看:8

问题描述

我有一些由 Drupal CMS 生成的内容,其中包含以下字符串:

I have some content that is generated by the Drupal CMS that contains strings like:

"... 
 Proficient knowledge of x3cstrongx3emedicalx3c/strongx3e terminology; typing skills at 40 wpm. Excellent communication and ... which involves access to sensitive and/or confidential x3cstrongx3emedicalx3c/strongx3e information. Must demonstrate leadership skills in decision making and ..."

我正在尝试将此数据作为 json 传输,但无法验证.我认为这是因为像 x3c 这样的字符可能需要采用较长的 6 个字符格式(参见 https://www.rfc-editor.org/rfc/rfc46272.5. - 字符串节).

I'm trying to transfer this data as json, but it doesn't validate. I think that's because the characters like x3c may need to be in the longer 6 character format (see https://www.rfc-editor.org/rfc/rfc4627, section 2.5. - Strings).

这真的是问题吗?如果是这样,有没有转换格式的好方法?

Is this actually the problem? And if so, is there a good way to convert the format?

这里有两个未通过验证的完整 json 样本

here are two full samples of the json that are failing validation

{ "results": [ { "link": "http://dev.careersatnmc.org/content/clinical-information-clerk-patient-financial-services-11-12", "type": "", "title": "Clinical Information Clerk, Patient Financial Services, #11-12", "user": "", "date": "1337699702", "node": "", "extra": "", "score": 1.9532660466727E+25, "snippet": "... 
 Proficient knowledge of x3cstrongx3emedicalx3c/strongx3e terminology; typing skills at 40 wpm. Excellent communication and ... which involves access to sensitive and/or confidential x3cstrongx3emedicalx3c/strongx3e information. Must demonstrate leadership skills in decision making and ..." }, { "link": "http://dev.careersatnmc.org/content/medical-assistant-northwestern-walk-clinic-11-44", "type": "", "title": "Medical Assistant, Northwestern Walk-In Clinic, #11-44", "user": "", "date": "1334178982", "node": "", "extra": "", "score": 1.6696042412062E+25, "snippet": "... 
 Yes 
 
 
 The x3cstrongx3eMedicalx3c/strongx3e Assistant performs patient screening care under the direction of the x3cstrongx3eMedicalx3c/strongx3e Director/On-site provider including, but not limited to, EKG’s. ..." }, { "link": "http://dev.careersatnmc.org/nursing-jobs", "type": "", "title": "Nursing Opportunities at Northwestern", "user": "", "date": "1333132723", "node": "", "extra": "", "score": 1.5935361158907E+25, "snippet": "... environment for caregivers. Here at Northwestern x3cstrongx3eMedicalx3c/strongx3e Center, in addition to being a destination of choice for patients, we ..." }, { "link": "http://dev.careersatnmc.org/nursing-careers/rn/registered-nurse-float-pool-11-106", "type": "", "title": "Registered Nurse, Float Pool #11-106 ", "user": "", "date": "1333040298", "node": "", "extra": "", "score": 1.5869853268872E+25, "snippet": "... safe nursing care in a timely manner to patients on the x3cstrongx3eMedicalx3c/strongx3e Surgical Unit and Intensive Care Units with a high degree of ... Float Pool RN will be required to rotate to both ICU and x3cstrongx3eMedicalx3c/strongx3e Surgical Units based on patient census and staffing need. These ..." }, { "link": "http://dev.careersatnmc.org/content/medical-assistant-northwestern-walk-clinic-11-68", "type": "", "title": "Medical Assistant, Northwestern Walk-In Clinic, #11-68", "user": "", "date": "1327941682", "node": "", "extra": "", "score": 1.2643954777586E+25, "snippet": "... 
 Yes 
 
 
 The x3cstrongx3eMedicalx3c/strongx3e Assistant performs patient screening care under the direction of the x3cstrongx3eMedicalx3c/strongx3e Director/On-site provider including, but not limited to, EKG’s. ..." }, { "link": "http://dev.careersatnmc.org/content/clinical-support-associate-diagnostic-imaging-10-126", "type": "", "title": "Clinical Support Associate, Diagnostic Imaging, #10-126", "user": "", "date": "1327936594", "node": "", "extra": "", "score": 1.2641087846662E+25, "snippet": "... 
 Three years experience in a x3cstrongx3emedicalx3c/strongx3e office required. Prior clerical work experience in a x3cstrongx3emedicalx3c/strongx3e office, knowledge of x3cstrongx3emedicalx3c/strongx3e terminology, typing skills required. ..." }, { "link": "http://dev.careersatnmc.org/content/licensed-practical-nurse-cardiology-11-61", "type": "", "title": "Licensed Practical Nurse, Cardiology, #11-61", "user": "", "date": "1327443988", "node": "", "extra": "", "score": 1.2366575548271E+25, "snippet": "... 
 Previous experience with electronic x3cstrongx3emedicalx3c/strongx3e records preferred. 
 
 
 
 Special Skills / ..." }, { "link": "http://dev.careersatnmc.org/equal-opportunity-policy", "type": "", "title": "Equal Opportunity", "user": "", "date": "1319564835", "node": "", "extra": "", "score": 8.704398538793E+24, "snippet": " Northwestern x3cstrongx3eMedicalx3c/strongx3e Center is an equal opportunity employer that is committed to fair and ..." }, { "link": "http://dev.careersatnmc.org/NMC-Hospital-Video", "type": "", "title": "NMC Hospital Video", "user": "", "date": "1317216552", "node": "", "extra": "", "score": 7.8394368227485E+24, "snippet": "... more about what itx26#39;s like to work at Northwestern x3cstrongx3eMedicalx3c/strongx3e Center from some of the hospitalx26#39;s providers. x26nbsp; 
 
 ..." }, { "link": "http://dev.careersatnmc.org/overview", "type": "", "title": "About NMC", "user": "", "date": "1305051468", "node": "", "extra": "", "score": 4.5584239764666E+24, "snippet": "... environment for caregivers.x26nbsp; Here at Northwestern x3cstrongx3eMedicalx3c/strongx3e Center, in addition to being a destination of choice for patients, we ..." } ], "total": "36" }

{ "results": [ { "link": "http://dev.northwesternmedicalcenter.org/courtyard-cafe", "type": "", "title": "The Courtyard Café", "user": "", "date": "1341844260", "node": "", "extra": "", "score": 0.54264448532277, "snippet": " Meals u0026amp; Snacks 
 The NMC Courtyard Café serves a wide variety of options, whether you need a full meal or just a snack or drink.u0026nbsp; There are always healthy options available to choose from in the Courtyard Café during hours of operation. 
 T ..." }, { "link": "http://dev.northwesternmedicalcenter.org/overview", "type": "", "title": "Welcome to Northwestern Medical Center!", "user": "", "date": "1308682802", "node": "", "extra": "", "score": 0.54083665338769, "snippet": " Northwestern u003cstrongu003eMedicalu003c/strongu003e Center is au0026nbsp;vibrant, not-for-profit, primary care hospital nestled ... we pride ourselves on bringing a broad range of high-tech u003cstrongu003emedicalu003c/strongu003e equipment u0026amp; services to our region. Thanks to that balance and the ..." }, { "link": "http://dev.northwesternmedicalcenter.org/stories-nmc", "type": "", "title": "Stories at NMC", "user": "", "date": "1340734687", "node": "", "extra": "", "score": 0.51676585442723, "snippet": "... Birth Center nurses and the IT folks at Northwestern u003cstrongu003eMedicalu003c/strongu003e Center, to experience the birth of his daughter Payton while on duty in ..." }, { "link": "http://dev.northwesternmedicalcenter.org/medical-executive-committee", "type": "", "title": "Medical Executive Committee", "user": "", "date": "1306856292", "node": "", "extra": "", "score": 0.41599960274235, "snippet": " u003cstrongu003eMedicalu003c/strongu003e Executive Committee 
 The NMC u003cstrongu003eMedicalu003c/strongu003e Staff is made up of more than 75 active staff physicians and more than 200 other physicians, dentists, and u003cstrongu003emedicalu003c/strongu003e providers who have privileges at the hospital.u0026nbsp; The u003cstrongu003eMedicalu003c/strongu003e Staff ..." }, { "link": "http://dev.northwesternmedicalcenter.org/medical-cardiology", "type": "", "title": "Medical Cardiology", "user": "", "date": "1327606268", "node": "", "extra": "", "score": 0.40720084861885, "snippet": " ..." }, { "link": "http://dev.northwesternmedicalcenter.org/news-and-updates/dr-lowrey-sullivan-named-chief-medical-officer", "type": "", "title": "Dr. Lowrey Sullivan Named Chief Medical Officer", "user": "", "date": "1326989520", "node": "", "extra": "", "score": 0.40509813494658, "snippet": "... that Dr. Sullivan has accepted the position of Chief u003cstrongu003eMedicalu003c/strongu003e Officer,u0026rdquo; said Jill Bowen, NMCu0026rsquo;s Chief Executive ... Having a physician who already has the respect of our u003cstrongu003emedicalu003c/strongu003e staff provides a strong foundation for the success of this ... his Bachelors degree from Middlebury College and his u003cstrongu003eMedicalu003c/strongu003e Degree from the University of Vermont.u0026nbsp; He did his Internship and ..." }, { "link": "http://dev.northwesternmedicalcenter.org/nmc.overview-video", "type": "", "title": "NMC Overview Video", "user": "", "date": "1327331110", "node": "", "extra": "", "score": 0.33907030714933, "snippet": " View the video below to learn more about St. Albanu0026rsquo;s lifestyle offerings. The city has much to offer and its central location between Burlington and Montreal makes it a great place to enjoy the pace and intimacy of a small town with access to bi ..." }, { "link": "http://dev.northwesternmedicalcenter.org/nmc-overview-video", "type": "", "title": "NMC Overview Video", "user": "", "date": "1327331316", "node": "", "extra": "", "score": 0.33905170147781, "snippet": " View the video below to learn more about St. Albanu0026rsquo;s lifestyle offerings. The city has much to offer and its central location between Burlington and Montreal makes it a great place to enjoy the pace and intimacy of a small town with access to bi ..." }, { "link": "http://dev.northwesternmedicalcenter.org/news-and-updates/test-story", "type": "", "title": "Test Story", "user": "", "date": "1326989380", "node": "", "extra": "", "score": 0.33538503005686, "snippet": " Story Details 
 Full Story:u0026nbsp; 
 
 
 Wolf cred veniam sunt. Nesciunt PBR four loko blog american apparel labore. Sint reprehenderit american apparel nihil, mcsweeneyu0026#39;s freegan voluptate velit al ..." }, { "link": "http://dev.northwesternmedicalcenter.org/news-and-updates/nmc-laboratory-featured-video", "type": "", "title": "NMC Laboratory Featured in a Video", "user": "", "date": "1326989494", "node": "", "extra": "", "score": 0.33522577107044, "snippet": " Story Details 
 Full Story:u0026nbsp; 
 
 
 This electronic approach, which is being used as a model throughout the state, is quicker, more efficient, more accurate, and less costly way of sharing informat ..." } ], "total": "236" }

推荐答案

x 通常代表十六进制,而u 代表unicode.您的问题与 Unicode 或 unicode 代码点无关.

x usually represents hexadecimal, while u is for unicode. Your question has nothing to do with Unicode or unicode codepoints.

使用 chr() 是安全的,因为 xFF 最大为 255 并且在 ASCII 范围内.

It is safe to use chr() because xFF is 255 max and that is in ASCII range.

function weird_answer_to_weird_question($string)
{
    return preg_replace_callback('#\\x([[:xdigit:]]{2})#ism', function($matches)
    {
        return chr(hexdec($matches[1]));
    },
    $string);
}

输出:

"... 精通医学术语;打字技巧在 40 wpm.优秀的沟通和...涉及访问敏感和/或机密医疗信息.必须表现出领导力决策能力和......"

"... Proficient knowledge of medical terminology; typing skills at 40 wpm. Excellent communication and ... which involves access to sensitive and/or confidential medical information. Must demonstrate leadership skills in decision making and ..."

附言

您还必须执行 $string = str_replace(' ', " ", $string); 或类似操作,因为 json_encode() 会双重编码那.感谢 @netcoder 指出.

You must also do a $string = str_replace(' ', " ", $string); or similar because json_encode() will double encode that. Thanks to @netcoder for pointing it out.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆