I have the following code that sanitizes post titles for saving the slug in my app:
$post_title = Sanitize::html($this->request->data['Post']['title'], array('remove'=>true, 'quotes' => ENT_NOQUOTES));
$post_title = String::truncate($post_title, 50, array('exact'=>false,'html'=>false,'ending'=>''));
$this->Post->saveField('slug', Inflector::slug($post_title));
Just in case anyone brings it up. I know of sluggable plugin but prefer to do it manually!
However ampersands are saved as ‘amp’ in the inflector. So I’m looking to convert ‘&’ to ‘and’ before the inflection stage.
How would I do this in PHP? I’ve looked at the str_replace function. Would this be correct?
e.g. str_replace('&','and',$post_title) but what about the count parameter? Any suggestions or ideas would be appreciated.
Thanks
str_replace('&','and',$post_title)is correct. You dont need the count parameter. This will replace all instances of&withand.I guess you want to do this before you truncate the string, because obviously
andtakes more charactors.