Generation

generate functionSun, 12 Feb 2023

write a php function to find the first h2 tag inside a php value and insert a div after this tag

function insert_after_h2($content){ if (is_single()){ $h2_pos = strpos($content, '<h2>'); if (false !== $h2_pos) { $h2_pos +=4; $h2_end = strpos($content, '</h2>'); $h2_len = $h2_end - $h2_pos; $h2_text = substr($content, $h2_pos, $h2_len); $replacement = '<div>' . $h2_text . '</div>'; $content = substr_replace($content, $replacement, $h2_end, 0); } return $content; } } add_filter('the_content', 'insert_after_h2');

Questions about programming?Chat with your personal AI assistant