After our discussion in chat, we managed to solve it as following:
Problem 1
Inserting html entities in the database and correctly displaying it again in html.
Before inputting the string to the database, use this function to convert the special characters to the correct entity codes:
$newstring = htmlentities( $oldstring , ENT_QUOTES, "UTF-8")
Then input
$newstring
into your database. It will have special characters like"
converted to"e;
.When retrieving the string from the database, it should automatically show the correct symbol (
"
) in the html, without you having to do anything!
If you are still having trouble with displaying the string, then you can check html_entity_decode().
Problem 2
Creating a function that works like substr()
, but works with words instead of single characters.
function subword($string, $amount){
$explode = explode(" ", $string);
$newstring = "";
for($i = 0; $i < $amount; $i++){
$newstring.= " ".$explode[$i];
}
return substr($newstring, 1);
};
$text = "This is a sentence with a lot of words and spaces!";
subword($text, 4); //This will give you the first 4 words "This is a sentence"