Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure string is a valid CSS ID name

Tags:

css

php

I have a bunch of database records (without auto_increment IDs or anything else like that) rendered as a list, and it came to pass that I need to differentiate each of them with a unique id.

I could just add a running counter into the loop and be done with it, but unfortunately this ID needs to be cross-referenceable throughout the site, however the list is ordered or filtered.

Therefore I got an idea to include the record title as a part of the id (with a prefix so it doesn't clash with layout elements).

How could I transform a string into an id name in a foolproof way so that it never contains characters that would break the HTML or not work as valid CSS selectors?

For example;

Title ==> prefix_title
TPS Report 2010 ==> prefix_tps_report_2010
Mike's "Proposal" ==> prefix_mikes_proposal
#53: Míguèl ==> prefix_53_miguel

The titles are always diverse enough to avoid conflicts, and will never contain any non-western characters.

Thanks!

like image 382
Emphram Stavanger Avatar asked May 16 '12 13:05

Emphram Stavanger


People also ask

How do I specify an ID in CSS?

The CSS id Selector The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

What is a valid CSS rule?

The :valid CSS pseudo-class represents any <input> or other <form> element whose contents validate successfully. This allows to easily make valid fields adopt an appearance that helps the user confirm that their data is formatted properly.

What is a valid CSS selector?

The :valid selector selects form elements with a value that validates according to the element's settings. Note: The :valid selector only works for form elements with limitations, such as input elements with min and max attributes, email fields with a legal email, or number fields with a numeric value, etc.

What is a valid HTML ID?

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). The HTML5 specification is a little more lax. It's only requirement is that “the value must not contain any space characters.”


1 Answers

I needed a similar solution for Deeplink Anchors.

Found this useful:

function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}

Credit: https://stackoverflow.com/a/11330527/3010027

PS: Wordpress-Users have a look here: https://codex.wordpress.org/Function_Reference/sanitize_title

like image 136
optimiertes Avatar answered Oct 22 '22 18:10

optimiertes