第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何為 <h2> 和 <h3> 標(biāo)簽創(chuàng)建 id 屬性,基于它們各自的 innerHTML 的一部分?

如何為 <h2> 和 <h3> 標(biāo)簽創(chuàng)建 id 屬性,基于它們各自的 innerHTML 的一部分?

PHP
慕容3067478 2022-07-29 16:44:14
我有一個(gè)使用 TinyMCE TOC 的客戶,但他不喜歡插件添加到標(biāo)題標(biāo)簽(<h2>和<h3>)的隨機(jī) ID。我想創(chuàng)建一個(gè)腳本來(lái)解析一篇文章,針對(duì)每個(gè) h2 和 h3 標(biāo)簽,然后id從它們包含的文本中創(chuàng)建屬性。我以為我可以用 來(lái)做到這一點(diǎn)preg_replace_callback(),但是當(dāng)我使用該功能時(shí),我意識(shí)到在某些情況下它不起作用。例如,如果 h2/h3 的文本以空格、數(shù)字等開頭,則它不起作用。這是在某些情況下有效的早期嘗試:function function_to_makeItClear($string) {    $string = strtolower($string);    $string = str_ireplace(' ', '-', $string);    return preg_replace('/[^A-Za-z0-9\-]/', '', $string); }function betterId($match){    $escape = str_split(strip_tags($match[2]), 20);    $id = strlen($escape[0]) >=5 ? function_to_makeItClear($escape[0]) : str_shuffle('AnyWordsHere');    return '<h'.$match[1].' id="'.$id.'">'.$match[2].'</h'.$match[1].'>';}return preg_replace_callback('#<h([1-6]).*?>(.*?)<\/h[1-6]>#si', 'betterId', $texte);這是我要轉(zhuǎn)換的一些示例文本:<p>Paragraph one is okay </p><h2>This will work without problem</h2><p>Paragraph two is okay </p><h2><a href="#">This heading has anchor</a></h2><p>Paragraph one is okay </p><h2>  This heading start with space</h2><p>Paragraph two is okay </p><h3>1. <a href="https://www.example1.com/">This wont work</a></h3><p>Paragraph one is okay </p><h3>2. <a href="https://www.example2.com/">Not working</a></h3><p>Paragraph two is okay </p><h3>3. Neither this one</h3><h3>But this works again</h3>我想要這個(gè)結(jié)果:<p>Paragraph one is okay </p><h2 id="this-will-work">This will work without problem</h2><p>Paragraph two is okay </p><h2 id="this-heading-has"><a href="#">This heading has anchor</a></h2><p>Paragraph one is okay </p><h2 id="this-heading-start">  This heading start with space</h2><p>Paragraph two is okay </p><h3 id="this-wont-work">1. <a href="https://www.example1.com/">This wont work</a></h3><p>Paragraph one is okay </p><h3 id="not-working">2. <a href="https://www.example2.com/">Not working</a></h3><p>Paragraph two is okay </p><h3 id="neighter-this-one">3. Neither this one</h3><h3 id="but-this-works">But this works again</h3>更新:從那以后,我使用 DOM 解析器實(shí)現(xiàn)了一種不同的方法,結(jié)果很好,但是仍然有一些失敗的場(chǎng)景,我必須自己手動(dòng)添加ids。
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊

使用 DOMDocument 及其好朋友 XPath 可靠地從您的有效 html 中提取標(biāo)題標(biāo)簽。

用于nodeValue()從標(biāo)題標(biāo)簽的 innerHTML 生成無(wú)標(biāo)簽字符串。(演示 nodeValue() 生成的內(nèi)容

用于preg_match()排除前導(dǎo)空格和數(shù)字,然后匹配第一個(gè)、兩個(gè)或三個(gè)單詞。(模式的稍微改變的演示

如果匹配項(xiàng)至少包含一個(gè)單詞,則用連字符替換空格并將該字符串添加為 id 屬性。

代碼:(演示

$html = <<<HTML

<div>

<p>Paragraph one is okay </p>

<h2>This will work without problem</h2>

<p>Paragraph two is okay </p>

<h2><a href="#">This heading has anchor</a></h2>

<p>Paragraph one is okay </p>

<h2>  This heading start with space</h2>

<p>Paragraph two is okay </p>

<h3>1. <a href="https://www.example1.com/">This wont work</a></h3>

<p>Paragraph one is okay </p>

<h3>2. <a href="https://www.example2.com/">Not working</a></h3>

<p>Paragraph two is okay </p>

<h3>3. Neither this one</h3>

<h3>But this works again</h3>

</div>

HTML;


$dom = new DOMDocument; 

$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($dom);

foreach ($xpath->query("//h2 | //h3") as $node) {

    if (preg_match('~^\s*(?:\d+\.)?\s*\K\S+(?:\s+\S+){1,2}~', $node->nodeValue, $m)) {

        $node->setAttribute('id', str_replace(' ', '-', strtolower($m[0])));

    }

}

echo $dom->saveHTML();

輸出:


<div>

<p>Paragraph one is okay </p>

<h2 id="this-will-work">This will work without problem</h2>

<p>Paragraph two is okay </p>

<h2 id="this-heading-has"><a href="#">This heading has anchor</a></h2>

<p>Paragraph one is okay </p>

<h2 id="this-heading-start">  This heading start with space</h2>

<p>Paragraph two is okay </p>

<h3 id="this-wont-work">1. <a href="https://www.example1.com/">This wont work</a></h3>

<p>Paragraph one is okay </p>

<h3 id="not-working">2. <a href="https://www.example2.com/">Not working</a></h3>

<p>Paragraph two is okay </p>

<h3 id="neither-this-one">3. Neither this one</h3>

<h3 id="but-this-works">But this works again</h3>

</div>


查看完整回答
反對(duì) 回復(fù) 2022-07-29
  • 1 回答
  • 0 關(guān)注
  • 214 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)