Javascript 清理jquery字符串?

Javascript 清理jquery字符串?,javascript,jquery,sanitization,Javascript,Jquery,Sanitization,在我的例子中,我使用jQuery和PHP。我可以添加数据宽度jQuery,当我添加数据宽度jQuery时,它会创建新的div标记 我的问题是,如果我将“Myåäötest”添加到一个div类中,例如,它看起来如下所示: <div class="My åäö test"> 事实上,这是三节课。我需要像Wordpress那样对它进行消毒: 我的结果应该是: <div class="my-aao-test"> 也欢迎您提供任何其他建议,以更好地存储附加到div或其

在我的例子中,我使用jQuery和PHP。我可以添加数据宽度jQuery,当我添加数据宽度jQuery时,它会创建新的div标记

我的问题是,如果我将“Myåäötest”添加到一个div类中,例如,它看起来如下所示:

<div class="My åäö test">

事实上,这是三节课。我需要像Wordpress那样对它进行消毒:

我的结果应该是:

<div class="my-aao-test">


也欢迎您提供任何其他建议,以更好地存储附加到div或其他元素的数据。

在将类分配给div之前,请使用
.replace()
例如:


$('myclass')。替换('''-')

将类分配给div之前,请使用
.replace()
例如:


$('myclass')。替换('''-')

为什么不使用Wordpress使用的相同功能? 您可以通过ajax将其传递以进行消毒,然后返回。 摘自来源:

function sanitize_title_with_dashes($title) {
    $title = strip_tags($title);
    // Preserve escaped octets.
    $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
    // Remove percent signs that are not part of an octet.
    $title = str_replace('%', '', $title);
    // Restore octets.
    $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);

    if (seems_utf8($title)) {
        if (function_exists('mb_strtolower')) {
            $title = mb_strtolower($title, 'UTF-8');
        }
        $title = utf8_uri_encode($title, 200);
    }

    $title = strtolower($title);
    $title = preg_replace('/&.+?;/', '', $title); // kill entities
    $title = str_replace('.', '-', $title);
    $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
    $title = preg_replace('/\s+/', '-', $title);
    $title = preg_replace('|-+|', '-', $title);
    $title = trim($title, '-');

    return $title;
}

为什么不使用Wordpress使用的相同功能? 您可以通过ajax将其传递以进行消毒,然后返回。 摘自来源:

function sanitize_title_with_dashes($title) {
    $title = strip_tags($title);
    // Preserve escaped octets.
    $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
    // Remove percent signs that are not part of an octet.
    $title = str_replace('%', '', $title);
    // Restore octets.
    $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);

    if (seems_utf8($title)) {
        if (function_exists('mb_strtolower')) {
            $title = mb_strtolower($title, 'UTF-8');
        }
        $title = utf8_uri_encode($title, 200);
    }

    $title = strtolower($title);
    $title = preg_replace('/&.+?;/', '', $title); // kill entities
    $title = str_replace('.', '-', $title);
    $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
    $title = preg_replace('/\s+/', '-', $title);
    $title = preg_replace('|-+|', '-', $title);
    $title = trim($title, '-');

    return $title;
}

你的意思是三个类,而不是两个元素?你为什么用类来代替html5
data
attributes?@Delan感谢你的输入。编辑了这篇文章@他们的主人我不知道。谢谢你的建议。投赞成票。你的意思是三个类,而不是两个元素?为什么你用类来代替html5
data
attributes?@Delan感谢你的输入。编辑了这篇文章@他们的主人我不知道。谢谢你的建议。投赞成票。当然,类还有其他非法字符,比如
等等。也许最好实现一个允许字符的白名单。@Jens Törnell;哈哈!好吧,不用担心,伙计。当然,类还有其他非法字符,比如
等等。也许最好实现一个允许字符的白名单。@Jens Törnell;哈哈!好吧,不用担心,伙计。谢谢!投票赞成那个。我可能会选择ThiefMaster方式,使用HTML5数据属性。谢谢!投票赞成那个。我可能会采用ThiefMaster的方式,使用HTML5数据属性。