在 300 字之后但在段落的结束标记之后的内容中插入文本 [英] Insert text in content after 300 words but after closing tag of a Paragraph

查看:22
本文介绍了在 300 字之后但在段落的结束标记之后的内容中插入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,可以在 X 个单词之后以及在最后一个单词出现的段落结束标记之后插入广告或文字.

I am looking for a way to insert an ad or text after X amount of words and after the closing tag of the paragraph the last word appears in.

到目前为止,我只能在 X 个字符后执行此操作.这种方法的问题在于会计算 HTML 字符,从而得出不准确的结果.

So far, I have only been able to do this after the X amount of characters. The problem with this approach is that HTML characters are counted which gives inaccurate results.

function chars1($content) {
    // only inject google ads if post is longer than 2500 characters
    $enable_length1 = 2500;
    // insert after the 210th character
    $after_character1 = 2100;

    if (is_single() && strlen($content) > $enable_length1) {
        $before_content1 = substr($content, 0, $after_character1);
        $after_content1 = substr($content, $after_character1);
        $after_content1 = explode('</p>', $after_content1);

        ob_start();
        dynamic_sidebar('single-image-ads-1');
        $text1 = ob_get_contents();
        ob_end_clean();

        array_splice($after_content1, 1, 0, $text1);
        $after_content1 = implode('', $after_content1);

        return $before_content1 . $after_content1;
    } else {
        return $content;
    }
}
//add filter to WordPress with priority 49
add_filter('the_content', 'chars1',49);

我尝试过的另一种方法是:

Another approach I have tried is using:

strip_tags($content)

并使用以下方法计算单词:

and counted the words using:

st_word_count()

问题在于我无法返回带有 HTML 标签的 $content

The problem with this is that I have no way of returning the $content with the HTML tags

根据帖子的大小,我最多可以插入 5 个广告单元,使用上面的功能,我需要为每个广告创建一个功能.如果有一种方法可以使用一个功能插入所有 5 个广告,那就太好了.

Depending on the size of the post, I will insert up to 5 ad units, with the functions I have above I would need to create a function for each ad. If there is a way to insert all 5 ads using one function that would be great.

感谢任何帮助.

推荐答案

决定一个词是什么还是不是一个词通常是非常困难的.但是,如果您对近似解决方案没问题,例如将单词定义为两个空格之间的文本,我建议您自己实现一个简单的函数.

Deciding what is a word or not can oftentimes be very hard. But if you're alright with an approximate solution, like defining a word as text between two whitespaces, I suggest you implement a simple function yourself.

这可以通过遍历字符串的字符直到计数到 150 个单词然后跳转到当前段落的末尾来实现.插入一个广告,然后重复,直到您添加了足够多的广告.

This may be achieved by iterating over the characters of the string until 150 words are counted and then jumping to the end of the current paragraph. Insert an ad and then repeat until you've added sufficiently many.

在你的函数中实现它可能看起来像这样

Implementing this in your function might look like this

function chars1($content) {
    // only inject google ads if post is longer than 2500 characters
    $enable_length1 = 2500;

    // Insert at the end of the paragraph every 300 words
    $after_word1 = 300;

    // Maximum of 5 ads
    $max_ads = 5;

    if (strlen($content) > $enable_length1) {
        $len = strlen($content);
        $i=0;

        // Keep adding untill end of content or $max_ads number of ads has ben inserted
        while($i<$len && $max_ads-->0) {
            // Work our way untill the apropriate length
            $word_cout = 0;
            $in_tag = false;
            while(++$i < $len && $word_cout < $after_word1) {
                if(!$in_tag && ctype_space($content[$i])) {
                    // Whitespace
                    $word_cout++;
                }
                else if(!$in_tag && $content[$i] == '<') {
                    // Begin tag
                    $in_tag = true;
                    $word_cout++;
                }
                else if($in_tag && $content[$i] == '>') {
                    // End tag
                    $in_tag = false;
                }
            }

            // Find the next '</p>'
            $i = strpos($content, "</p>", $i);
            if($i === false) {
                // No more paragraph endings
                break;
            }
            else {
                // Add the length of </p>
                $i += 4;

                // Get ad as string
                ob_start();
                dynamic_sidebar('single-image-ads-1');
                $ad = ob_get_contents();
                ob_end_clean();

                $content = substr($content, 0, $i) . $ad . substr($content, $i);

                // Set the correct i
                $i+= strlen($ad);
            }
        }
    }

    return $content;
}

使用这种方法,可以轻松添加新规则.

With this approach, it's easy to add new rules.

这篇关于在 300 字之后但在段落的结束标记之后的内容中插入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆