News:

Join the Facebook Fan Page.

Main Menu

Regex Expressions

Started by ziycon, August 29, 2012, 07:48:27 AM

Previous topic - Next topic

ziycon

I have the below regex expresion but it's not removing apostrophes, can anyone tell me how to add it to the pattern I'm using, I've tried everything at this stage?

$title = preg_replace('#[^a-zA-Z0-9]+#','-',$title);

MrPhil

Your preg_replace() call should be replacing each run of one or more non-alphanumerics with a single hyphen (-). Are you saying that it misses certain characters? Can you give an example of a $title that is not being properly handled? What character set encoding are you using? Is there any chance that these apostrophes are actually MS Smart Quotes (pasted in from Word or Outlook)? If so, it's possible that the library is deliberately skipping them as control characters. That behavior may be different between Latin-1 and UTF-8 encodings.

ziycon

Database and all fields are set to UTF-8 and the page encoding is also UTF-8, the below string is being changed to the string under it.

Before:
This isn't a test string is it?

After:
This-isn-t-a-test-string-is-it-

Should be:
This-isnt-a-test-string-is-it

Don't worry about a hyphen as the first or last character as I can easily remove these.

Suki

If you want to remove the ' instead of changing it to - then remove it before passing the string to preg_replace()

the regex is doing what is suppose to do which is turning into - anything that is not a letter or a number, it won't remove the ', you have to do that separately.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Advertisement: