How to use single quote inside an echo which is using single quote
Have you tried the function addslashes()? It even uses your example.
I personally prefer the function htmlspecialchars() which does the same thing but has flags which let you specify its behavior.
like so:
echo htmlspecialchars("O'Rielly", ENT_QUOTES);
This shows the string properly on an HTML webpage.