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

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

I would use whitelisting instead of blacklisting characters.

/[^a-z]/i

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.