將字符串轉換為類的安全名稱 (Convert string to safe name for class)


問題描述

將字符串轉換為類的安全名稱 (Convert string to safe name for class)

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'

‑‑‑‑‑

參考解法

方法 1:

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.

方法 2:

Have you tried preg_replace?

This will return 'ProductsSunflowers' for your above example.

preg_replace('#\W#g','',$className);

方法 3:

BEM‑style clean_class php solution

Shamelessly interpolated from Drupal 7's drupal_clean_css_identifier() and drupal_html_class() functions.

/**
 * Convert any random string into a classname following conventions.
 * 
 * ‑ preserve valid characters, numbers and unicode alphabet
 * ‑ preserve already‑formatted BEM‑style classnames
 * ‑ convert to lowercase
 *
 * @see http://getbem.com/
 */
function clean_class($identifier) {

  // Convert or strip certain special characters, by convention.
  $filter = [
    ' ' => '‑',
    '__' => '__', // preserve BEM‑style double‑underscores
    '_' => '‑', // otherwise, convert single underscore to dash
    '/' => '‑',
    '[' => '‑',
    ']' => '',
  ];
  $identifier = strtr($identifier, $filter);

  // Valid characters in a CSS identifier are:
  // ‑ the hyphen (U+002D)
  // ‑ a‑z (U+0030 ‑ U+0039)
  // ‑ A‑Z (U+0041 ‑ U+005A)
  // ‑ the underscore (U+005F)
  // ‑ 0‑9 (U+0061 ‑ U+007A)
  // ‑ ISO 10646 characters U+00A1 and higher
  // We strip out any character not in the above list.
  $identifier = preg_replace('/[^\\x{002D}\\x{0030}‑\\x{0039}\\x{0041}‑\\x{005A}\\x{005F}\\x{0061}‑\\x{007A}\\x{00A1}‑\\x{FFFF}]/u', '', $identifier);

  // Convert everything to lower case.
  return strtolower($identifier);
}

方法 4:

I wrote a sample code to solve your problem, hope it helps

<?php
 # filename s.php
 $r ='@_+(\w)@';
$a='a_bf_csdfc__dasdf';
$b= ucfirst(preg_replace_callback(
    $r,
    function ($matches) {
        return strtoupper($matches[1]);
    },
        $a
    ));
echo $a,PHP_EOL;
echo $b,PHP_EOL;
  

$ php ‑f s.php 

     

a_bf_csdfc__dasdf

     

ABfCsdfcDasdf

方法 5:

string: dome/some.thing‑to.uppercase.words

result: DomeSomeThingToUppercaseWords

(add your pattern ofc)

var_dump(
    str_replace(
        ['/', '‑', '.'], 
        '', 
        ucwords(
            $acceptContentType, '/‑.'
        )
    )
);

(by John MagnoliatsicwallenpooleJamesWilsonren8zhucottton)

參考文件

  1. Convert string to safe name for class (CC BY‑SA 3.0/4.0)

#css #PHP






相關問題

在圖像上淡入文本 (Fade in text over an image)

可以使用 css3 轉換輪廓顏色 (possible to transition outline color with css3)

以背景色為條件的 if 語句 (If statement with background color as condition)

Nội dung từ <p> biến mất khi tôi quay lại trang (Content from <p> disappears when I return to the page)

當按鈕向右浮動時,ng-click 不起作用 (ng-click doesn't work when the button is floated to the right)

帶有偽 css 的背景顏色層 (Background color layer with pseudo css)

是否可以製作離線工作的網絡應用程序? (Is it possible to make a web-app which works offline?)

chrome中帶有省略號的多行文本問題 (issue with multiline text with ellipsis in chrome)

將字符串轉換為類的安全名稱 (Convert string to safe name for class)

絕對定位跨度收縮 (Absolute Positioned Span Shrinks)

如何設置單選按鈕的樣式,使其看起來像普通的可點擊按鈕? (How do I style a radio button to look like a normal clickable button?)

無法獲得白色和灰色的 CSS 進度條 (Not able to get the CSS progress bar with white and grey)







留言討論