Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为<;创建id属性;h2>;及<;h3>;基于各自innerHTML的一部分的标记?_Php_Html - Fatal编程技术网

Php 如何为<;创建id属性;h2>;及<;h3>;基于各自innerHTML的一部分的标记?

Php 如何为<;创建id属性;h2>;及<;h3>;基于各自innerHTML的一部分的标记?,php,html,Php,Html,我有一个客户使用TinyMCE TOC,但他不喜欢插件添加到标题标签中的随机ID(和) 我想创建一个脚本来解析一篇文章,针对每个h2和h3标记,然后从它们包含的文本中创建id属性 我想我可以用preg\u replace\u callback()来实现这一点,但当我使用该函数时,我意识到在某些情况下它不起作用 例如,如果h2/h3的文本以空格、数字等开头,则不起作用 以下是在某些情况下成功的早期尝试: function function_to_makeItClear($string) {

我有一个客户使用TinyMCE TOC,但他不喜欢插件添加到标题标签中的随机ID(

我想创建一个脚本来解析一篇文章,针对每个h2和h3标记,然后从它们包含的文本中创建
id
属性

我想我可以用
preg\u replace\u callback()
来实现这一点,但当我使用该函数时,我意识到在某些情况下它不起作用

例如,如果h2/h3的文本以空格、数字等开头,则不起作用

以下是在某些情况下成功的早期尝试:

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);
function-to-makeItClear($string){
$string=strtolower($string);
$string=str_ireplace(“”,“-”,$string);
返回预替换('/[^A-Za-z0-9\-]/',''$string);
}
函数betterId($match){
$escape=str_split(strip_标签($match[2]),20);
$id=strlen($escape[0])>=5?清除($escape[0]):str_shuffle('AnyWordsHere');
返回“.$match[2]”;
}
返回preg#u replace_回调('#(.*)#si',betterId',$texte);
以下是我要转换的一些示例文本:

<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>
第一段可以

这将毫无问题地起作用 第二段可以

第一段可以

此标题以空格开头 第二段可以

1. 第一段可以

2. 第二段可以

3.这两个都不是 但这又起作用了
我希望得到以下结果:

<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>
第一段可以

这将毫无问题地起作用 第二段可以

第一段可以

此标题以空格开头 第二段可以

1. 第一段可以

2. 第二段可以

3.这两个都不是 但这又起作用了
更新:


此后,我使用DOM解析器实现了一种不同的方法,取得了很好的效果,但仍有一些情况下它会失败,我必须自己手动添加
id
s。

使用DOMDocument及其好友XPath从有效的html中可靠地提取标题标记

使用
nodeValue()
从标题标记的innerHTML生成无标记字符串。()

使用
preg_match()
排除前导空格和数字,然后匹配第一个、两个或三个单词。()

如果存在至少包含一个单词的匹配项,请将空格替换为连字符,并将该字符串添加为id属性

代码:()

$html=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>

第一段可以

这将毫无问题地起作用 第二段可以

第一段可以

此标题以空格开头 第二段可以

1. 第一段可以

2. 第二段可以

3.这两个都不是 但这又起作用了
你好@mickmackusa!如果只包含一个单词,则代码段不会捕获文本。例如,Google不会创建ID。您能告诉我如何更改或添加现有模式吗?
<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>