Generating XML document in PHP (escape characters)

前端 未结 10 991
难免孤独
难免孤独 2020-11-27 04:20

I\'m generating an XML document from a PHP script and I need to escape the XML special characters. I know the list of characters that should be escaped; but what is the corr

10条回答
  •  爱一瞬间的悲伤
    2020-11-27 04:54

    Based on the solution of sadeghj the following code worked for me:

    /**
     * @param $arr1 the single string that shall be masked
     * @return the resulting string with the masked characters
     */
    function replace_char($arr1)
    {
        if (strpos ($arr1,'&')!== FALSE) { //test if the character appears 
            $arr1=preg_replace('/&/','&', $arr1); // do this first
        }
    
        // just encode the
        if (strpos ($arr1,'>')!== FALSE) {
            $arr1=preg_replace('/>/','>', $arr1);
        }
        if (strpos ($arr1,'<')!== FALSE) {
            $arr1=preg_replace('/

提交回复
热议问题