0

I am currently doing this:

$a = 'some string with special characters';
$a = htmlentities($a);
$a = trim(preg_replace('/&#?[a-z0-9]+;/i', '', $a));

This works but I am wondering if there's a more efficient way to do this?

IMB
  • 15,163
  • 19
  • 82
  • 140

1 Answers1

2

it contains certain characters like  and I want to remove those.

Try utf8_decode, or more feature-filled:

$output = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $input);

Documentation

The reason I bring this up is because what you are seeing are encoding issues, not "special characters".

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Do you happen to know how to apply that in `phpQuery`? More specifically, I am using `phpQuery::newDocumentFile` to scrape an HTML page. – IMB May 07 '14 at 16:41
  • I couldn't remember the name of the function, but this is what I was going to suggest. Converting the "special characters" to their similar counterpart wouldn't destroy the original text as much. – Jonathan Kuhn May 07 '14 at 16:41
  • Nope. No idea. But if there's any kind of encoding setting somewhere then you should set that to UTF-8 (instead of using the code in this answer - because this code is somewhat destructive) – Niet the Dark Absol May 07 '14 at 16:42
  • The OP clearly states that he wants to completely wipe out "special" characters (whatever that means), not decode them properly or convert to his application's encoding. Yet you have my +1 because I simply can't believe him :) – Álvaro González May 07 '14 at 16:54
  • @ÁlvaroG.Vicario Pfft, had me worried there! – Niet the Dark Absol May 07 '14 at 17:18