How to escape special characters in php?

The function converts special characters into HTML entities, (html will no longer work so you can echo it outside).

1. Code:

    <?php 
	function escapeSpecialChars($str){
		return htmlspecialchars($str, 1, "utf-8");
	}
    $str = '<?xml version="1.0" encoding="UTF-8"?>
<Tutorials>
    <Post>
        <Title>
             Soltuts
        </Title>
        <Code>
            <![CDATA[ 
            <?xml version="1.0" encoding="UTF-8"?>
            <Persons>
                <Person>
                    Soltuts
                </Person>
            </Persons>
            ]]>
        </Code>
    </Post>
</Tutorials>';

	echo escapeSpecialChars($str);
	echo "<br>";	
    ?> 

2. Example:

Input:

$str = '<?xml version="1.0" encoding="UTF-8"?>
<Tutorials>
    <Post>
        <Title>
            Soltuts
        </Title>
        <Code>
            <![CDATA[ 
            <?xml version="1.0" encoding="UTF-8"?>
            <Persons>
                <Person>
                   Soltuts 
                </Person>
            </Persons>
            ]]>
        </Code>
    </Post>
</Tutorials>';

Output:

<?xml version="1.0" encoding="UTF-8"?> <Tutorials> <Post> <Title>  Soltuts </Title> <Code> <![CDATA[ <?xml version="1.0" encoding="UTF-8"?> <Persons> <Person>  Soltuts </Person> </Persons> ]]> </Code> </Post> </Tutorials>