最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Replace an array (with identical values) with another array in the_content

programmeradmin1浏览0评论

I'm making a plugin where I need to edit the H2 tags in the post content.

I've been at it for two days, but my php knowledge is too limited to solve the issue..

What I want to achieve is to return all <h2> tags as <h2 id="$headings_array_stripped">

What I can't seem to figure out is how to do this. I've tried combinations with foreach loops and preg_replace, str_replace and sorts.

The issue seems that the values inside the $headings_open_tag_array are all the same (<h2>) , so str_replace, for example, won't cycle through them, but only changes all <h2>'s to the first corresponding value of $headings_array_stripped:

str_replace( $headings_open_tag_array, $headingsarray_stripped, $article )

The code:

function replace_content_jump_menu() {

// Get metadata attached to the plugin
$jump_menu_check = get_post_meta( get_the_ID(), 'jump-menu', true );

// Get the content
$article = get_the_content();

// Retrieve all <h2>header</h2> tags (with content) and put them in an array
preg_match_all( '|<h2>(.*)</h2>|iU', $article, $headings_array ); 

// Strip h2 tags and make lowercase
$headings_array_stripped = preg_replace( '/\W+|[0-9]+|\<h2\>|\<\/h2\>/', '', array_map( 'strtolower', $headings_array[0] ) ); 

// Retrieve all <h2> tags from the_content and put them in an array
preg_match_all( '|<h2>|iU', $article, $headings_open_tag_array ); 

// preg_replace? str_replace? foreach()?

};
add_filter( 'the_content', 'replace_content_jump_menu' );

I'm making a plugin where I need to edit the H2 tags in the post content.

I've been at it for two days, but my php knowledge is too limited to solve the issue..

What I want to achieve is to return all <h2> tags as <h2 id="$headings_array_stripped">

What I can't seem to figure out is how to do this. I've tried combinations with foreach loops and preg_replace, str_replace and sorts.

The issue seems that the values inside the $headings_open_tag_array are all the same (<h2>) , so str_replace, for example, won't cycle through them, but only changes all <h2>'s to the first corresponding value of $headings_array_stripped:

str_replace( $headings_open_tag_array, $headingsarray_stripped, $article )

The code:

function replace_content_jump_menu() {

// Get metadata attached to the plugin
$jump_menu_check = get_post_meta( get_the_ID(), 'jump-menu', true );

// Get the content
$article = get_the_content();

// Retrieve all <h2>header</h2> tags (with content) and put them in an array
preg_match_all( '|<h2>(.*)</h2>|iU', $article, $headings_array ); 

// Strip h2 tags and make lowercase
$headings_array_stripped = preg_replace( '/\W+|[0-9]+|\<h2\>|\<\/h2\>/', '', array_map( 'strtolower', $headings_array[0] ) ); 

// Retrieve all <h2> tags from the_content and put them in an array
preg_match_all( '|<h2>|iU', $article, $headings_open_tag_array ); 

// preg_replace? str_replace? foreach()?

};
add_filter( 'the_content', 'replace_content_jump_menu' );
Share Improve this question edited Jul 1, 2019 at 7:31 Erick Arends asked Jul 1, 2019 at 7:15 Erick ArendsErick Arends 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

I found this article by Jeroen Sormani which solved my problem

发布评论

评论列表(0)

  1. 暂无评论