因此,例如,对于每个标题唯一的帖子,它将设置为“1”,但如果标题再次重复,它将设置为“2”,依此类推。。。
使用array_count_values()
在数组中放置标题后。使用the_title_attribute()
使用false
获取esc_attr()
&;标记已删除的标题。注意与一起使用的regexpreg_replace
或者,最终可能会出现错误匹配。设置后array_count_values()
, foreach循环使用as $key => $value
可以使用complex syntax 创建变量名称的步骤$key
(又名post_title
): ${\'_\'. $key} = $value
.
拥有所有post对象后,循环浏览它们。
获取标题,您可以使用the_title_attribute()
, 因为它贯穿整个标题esc_attr();
$title_cleaned = the_title_attribute( array( \'echo\' => false, );
//OR: $post->post_title for raw from object
旁注:也可以从对象中获取原始标题
$post->post_title
foreach ($posts as $post) {
$title_cleaned = esc_attr( $post->post_title )
}
修剪、消毒、预替换删除任何前导或结尾的额外空格,并强制键入字符串
$title_cleaned = (string) trim( $post );
删除所有非字母数字空格,保留空格
$title_cleaned = preg_replace( \'/[^a-zA-Z0-9 ]*/\', \'\', $title_cleaned );
将空格更改为下划线
$title_cleaned = preg_replace( \'/\\s+/\', \'_\', $title_cleaned );
将值强制转换为数组
$array_of_titles[] = $title_cleaned;
//循环结束
获取计数使用array_count_values()
铸造key => values
属于post_title => count
$counted_array = array_count_values( $array_of_titles );
的价值
$counted_array
将类似于:
Array (
[ Style ] => 3
[ Dog ] => 1
)
设置${\'..$键}=$值为变量名称设置键,添加下划线以避免以数字开头的变量名称
foreach( $counted_array as $key => $value ) {
${\'_\'. $key} = (int) $value;
}
以上所有内容的示例:
foreach ($posts as $post ) {
$title_cleaned = the_title_attribute( array( \'echo\' => false, );
$title_cleaned = (string) trim( $title_cleaned );
$title_cleaned = preg_replace( \'/[^a-zA-Z0-9 ]*/\', \'\', $title_cleaned );
$title_cleaned = preg_replace( \'/\\s+/\', \'_\', $title_cleaned );
$array_of_titles[] = $title_cleaned;
}
$counted_array = array_count_values( $array_of_titles );
foreach( $counted_array as $key => $value ) {
${\'_\'. $key} = (int) $value;
}
这个php文件可以在WP之外使用,以了解我对regex内容标题的理解。
<?php
//using array below as a mock response of titles after passing through esc_attr
$faux_the_title_attribute = array( \' My Styles & Colors \', \'My Style's Colors\', \'My Styles: Colors\', \'My Styles: Colors\', \'My Styles: Colors\' );
foreach ($faux_the_title_attribute as $pt ) {
//$title_cleaned = the_title_attribute( array( \'echo\' => false, );
$title_cleaned = (string) trim( $pt );
$title_cleaned = preg_replace( \'/[^a-zA-Z0-9 ]*/\', \'\', $title_cleaned );
$title_cleaned = preg_replace( \'/\\s+/\', \'_\', $title_cleaned );
//My Styles & Colors = My_Styles_amp_Colors
$array_of_titles[] = $title_cleaned;
}
$counted_array = array_count_values( $array_of_titles );
foreach( $counted_array as $key => $value ) {
// ${\'_\'. $key} = $key . \'_\' . $value;
//becomes $_My_Styles_amp_Colors = _My_Styles_amp_Colors_3;
${\'_\'. $key} = (int) $value;
//becomes $_My_Styles_Colors = 3;
}
printf( $_My_Styles_amp_Colors );
?>
关于regex的注释,将标题字符串发布到
${$title}
, 以及潜在的问题,如果发生
post_title
从数字开始,自
php variables must begin with a letter or underscore.
取决于你如何获得标题,get_title()
由以下人员调用the_title()
, 或来自的对象get_post
可能未应用筛选器。
而正则表达式[^a-zA-Z0-9]*
将删除所有非字母数字,无esc_attr()
到htmlencode
首先是他们(即get_post()
使用默认值raw
过滤器),返回的字符串“我的样式和颜色”、“我的样式的颜色”或“我的样式:颜色”都是MyStylesColors
在preg_replace
.
但之后esc_attr()
:
My Styles & Colors = My Styles & Colors
My Style\'s Colors = My Style's Colors
My Styles: Colors = My Styles: Colors
之后
esc_attr()
和regex
preg_replace
:
My Styles & Colors = MyStylesampColors
My Style\'s Colors = MyStyle039sColors
My Styles: Colors = MyStylesColors
但是,删除表达式中的空白(通过不包括它,即。
[^a-zA-Z0-9 ]*
), 导致以下可能的问题:
My Styles & le Colors = MyStylesampleColors
My Style sample Colors = MyStylesampleColors
在已批准的正则表达式中添加空格,并使用第二个
preg_replace(\'/\\s+/\', \'_\', $string);
要将空格替换为下划线,请执行以下操作:
$title_cleaned = (string) the_title_attribute( array( \'echo\' => false );
$title_cleaned = preg_replace( \'/[^a-zA-Z0-9]/\', \'\', $title_cleaned );
$title_cleaned = preg_replace( \'/\\s+/\', \'_\', $title_cleaned );
//My Styles & Colors = My_Styles_amp_Colors
但这会将开头或结尾的空格转换为下划线,所以php的
trim()
在任何
preg_replace
.
所有这些都不强制变量名的开头为字母或下划线(数字对于启动php变量无效)。
因此,文章标题:
3 Best Styles = 3BestStyles = $3BestStyles
您可以根据
^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$
, 或者在所有内容前面加下划线。所以从上面继续
$title_cleaned
示例:
$title_cleaned = the_title_attribute( array( \'echo\' => false, );
$title_cleaned = (string) trim( $title_cleaned );
$title_cleaned = preg_replace( \'/[^a-zA-Z0-9 ]*/\', \'\', $title_cleaned );
$title_cleaned = preg_replace( \'/\\s+/\', \'_\', $title_cleaned );
$array_of_titles[] = $title_cleaned;
这里可能还有其他问题没有考虑在内。
就个人而言,如果只需要知道这些计数并能够比较、排序等,我会在以下步骤停止$counted_array = array_count_values( $array_of_titles );
放弃动态可变位。当然是国际海事组织。