簡體   English   中英

HTML中的字符串在PHP中用json_encode無效

[英]String with HTML is nulled with json_encode in PHP

我有一個數組,並且數組中的一個元素具有帶HTML標簽的字符串。 當我遍歷數組並回顯字段時,我可以看到字符串。

    $positions = $jobs_dbc->getData($sql);
    foreach($positions as $position) {
        echo $position['PositionDescription'];
    }

在下面的示例中,$ positions數組中總共有3個元素。 前兩個在PositionDescription中沒有HTML標記。 第三條記錄可以,並且我強調了上面代碼的結果:

帶有html標簽的字符串

但是-當我對數組進行json_encode時,該第三條記錄的PositionDescription顯示為NULL。

echo json_encode($positions, JSON_HEX_TAG|JSON_HEX_QUOT);

上面的代碼返回: json編碼結果

如何獲取HTML編碼的字符串?

更新:

var_export($positions)
array ( 0 => array ( 'PositionName' => 'Front End Sales', 'PositionManagement' => '0', 'PositionDescription' => 'Works in scratch bakery department to provide guest care, package, stock & merchandise fresh bakery products. Responsible for accurate rotation of products & recording sales & loss. Will take in-person & phone orders for special occasion cakes & other bakery products.', 'DepartmentName' => 'Bakery', ), 1 => array ( 'PositionName' => 'Baker', 'PositionManagement' => '0', 'PositionDescription' => 'Works in scratch bakery department to provide guest service & produce donuts, pastries & bread. Will use variety of machines to assist with meeting production goals including deep fryer, sheeter, mixer, scale and oven.', 'DepartmentName' => 'Bakery', ), 2 => array ( 'PositionName' => 'Part-time Accounting Clerk I', 'PositionManagement' => '0', 'PositionDescription' => 'APPLY BY FRIDAY 10/9/09! PART-TIME = 25 hours/wk, flex shifts M-F between 8am-4pm. Position: Accounting Clerk I Department: Accounting Reports To: Accounting Manager Position Summary: The Accounting Clerk I assists other Accounting Clerks with communication between accounts payable and the vendor community ensuring that all related documentation and paperwork is completed in a timely and accurate manner. They also assist in the processing of past due statements creating a trusted relationship between the accounting department and the vendors we partner with. Responsibilities: 1. Provides world-class service and mentors all co-workers towards providing the same passionate service at all times. 2. Researches and reviews vendor statements in order to facilitate the prompt processing of accounts payable activity. 3. Provides recap of vendor coupon totals received from the stores and prepares coupon bundles for mailing to coupon processing center. 4. Works in and cultivates a team oriented work environment by sharing work knowledge and offering assistance to co-workers as needed including vacation coverage. 5. Maintains confidentiality of departmental/company data, initiatives and other sensitive information. 6. Provides support to the stores by answering questions and troubleshooting accounts payable issues as they develop. 7. Adheres to all company policies and procedures creating a professional and enjoyable work environment. Qualifications: •Adept with PC based software applications including MS Office •Demonstrated 10-key by touch and proficient keyboarding skills •Strong organization and problem solving skills •Exhibits passionate commitment to detail and accuracy •Math skills necessary, but not limited to: reading and interpreting reports •Excellent communication skills with the ability to communicate with co-workers and other crew members in English, demonstrate professional phone etiquette, courteously answer telephone calls and promptly respond to voicemail/email messages •Must be at least 18 years of age due to equipment use and work environment Work Environment: Work space will be primarily in office environment in an assigned cubicle. General office schedule is Monday-Friday with flexible work hours between 8am-4pm. Typically a part-time position. Physical activity: Sitting, fingering, standing/walking, bending, twisting, reaching, pushing/pulling Tools and Equipment: Computer, printer, scanner, telephone, 10-key calculator, fax machine, copier. This job description is representative only and is not exhaustive of the tasks that a crew member in the position may be required to perform. Haggen, Inc. reserves the right to request and require its crew members to perform other tasks depending on circumstances or emergencies. ', 'DepartmentName' => 'Corporate', ), )

我以為PositionDescription有HTML標記,但是一旦查看數據庫中的數據,我就會看到像項目符號這樣的字符,它們不是HTML標記,而是實際的符號。

json_encode必須為utf8字符。 將字段轉換為utf8后,json_encode開始返回字符串。

foreach($positions as &$position) {
        $position['PositionDescription'] = utf8_encode($position['PositionDescription']);
    }

echo json_encode($positions, JSON_HEX_TAG|JSON_HEX_QUOT);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM