Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to safe name for class

Tags:

css

php

I have a dynamic menu that I need to convert into background images using CSS classes. I would like to convert the label into a safe class name for the css.

An example being: - Convert string: 'Products & Sunflowers' - Into a string that contains only a-z and 1-9. The above would be converted into a validate string that can be used as a class name eg: 'products_sunflowers'

like image 791
John Magnolia Avatar asked Jun 30 '11 09:06

John Magnolia


1 Answers

I use this:

preg_replace('/\W+/','',strtolower(strip_tags($className)));

It will strip all but letters, convert to lower-case and remove all html tags.

like image 121
tsi Avatar answered Oct 13 '22 20:10

tsi