Search code examples
phpsubstrhtmlspecialchars

how can set substr for special chars?


how can I substr a text that execute htmlspecialchars to it.

saved in database

some text " some text " some text
' some text ' some text 'some text' some text '
some text 'some text ' some text ' some text '

with htmlspecialchars ( I want to substr from this text )

some text " some text " some text ' some text ' some text ' some text '
some text ' some text ' some text ' some text ' some text '

when substr :

some text " some text " some text ' some text �

Tanks

UPDATE

I use this

 if( strlen($text)>100){
    $s = html_entity_decode($text);
    $shorten = substr($s,0,50);
    echo htmlentities($shorten);
  }

and just changes "


Solution

  • Try decode before substr and encode after:

    $str = htmlentities(
       substr(html_entity_decode($str, ENT_QUOTES),0,50)
    ,ENT_QUOTES);
    

    Test at eval.in (link expires soon)