Reuben
Reuben

Reputation: 4276

How to add non word characters to Inflector::slug() for CakePHP 1.2?

The CakePHP 1.2 Inflector class has a slug() method that converts spaces to underscores, converts accented characters to non-accented characters and non word characters are removed.

I have a case where special doubled quotes [“ and ”] are not getting removed.

I can see the line where I need to place the characters for the quotes, but I don't know what tool to use to put the characters in to a format where they will be recognised correctly. (i.e. ’ is a special single quote that gets converted to a space and ultimately removed).

Upvotes: 0

Views: 498

Answers (2)

Reuben
Reuben

Reputation: 4276

My eventual work around was to use the Encoding feature in Chrome to get the characters I needed. Normally, the web page has the UTF-8 charset, so I forced Chrome to display in ISO-8859-1, which transformed “ in to “. It's Menu, Tools, Encoding and then select the encoding.

When it came to actually altering Inflector::slug(), the '/[^\w\s]/' => ' ', line is changed to '/–|“|’|â€|[^\w\s]/' => ' ',.

I've made sure to convert [^\w\s] last, and †second last. If [^\w\s] is earlier, then € gets converted to a space, and other special characters using that don't get converted. And there's a similar deal with â€.

The replacement string used is not exhaustive. Currently it caters for a special dash, special inverted double quote, special double quote and special single quote.

Upvotes: 0

Kevin Vandenborne
Kevin Vandenborne

Reputation: 1387

You could remove non-ascii characters before pusing it into the inflector.

<?php $output = preg_replace('/[^(\x20-\x7F)]*/','', $input); ?>

Upvotes: 0

Related Questions