0

I am getting a PHP error while doing a preg_replace.

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 24 in xxx/item.php on line 53

Line# 53 is this..

$itemid = substr(trim(strtolower(preg_replace('/[0-9_%:\[(&#@!~*).\]\\/-\s+]/','',$rsstitle))), 0, 8);

Basically I am trying to omit all the characters except alphabets. What I am doing wrong? Is there any better and faster/better way of doing this?

Tried several answers suggested here while posting this, but none of them worked.

Thanks

Abhik
  • 664
  • 3
  • 22
  • 40

2 Answers2

3

The reason for the error is that the dash (-) at char pos 24 is being interpreted as a range identifier (e.g. as in 0-9 means any numeric character between 0 and 9) but / to white space cannot be interpreted as a meaningful range. Probably best to escape the dash or possibly move it to the end of your character string.

The suggestion to use whitelisting is probably good practice but the same error could just as easily occur if dashes are permitted characters in a given whitelist.

izboo
  • 31
  • 2
2

I would use whitelisting instead of blacklisting characters.

/[^a-z]/i

This will match to all characters except a...z

Niko Hujanen
  • 743
  • 5
  • 10