dongxiaoshe0737 2019-05-28 18:24
浏览 196

PHP - 是否可以在变量中添加字符串?

I would like to make a small change in a code for a table of contents.

I want to add a sign in front of each heading. The character should be recognized as text.

I've tried a few things, but unfortunately I have not found the right variable.

The code comes from a plugin for Wordpress

I have already tried the following variables:

$items
$tic
$find
$replace
$post

Here is the code that prints the list:

if ( $tic->is_eligible($custom_toc_position) ) {

                extract( $args );

                $items = $tic->extract_headings( $find, $replace,wptexturize($post->post_content) );
                $title = ( array_key_exists('title', $instance) ) ? apply_filters('widget_title', $instance['title']) : '';
                if ( strpos($title, '%PAGE_TITLE%') !== false ) $title = str_replace( '%PAGE_TITLE%', get_the_title(), $title );
                if ( strpos($title, '%PAGE_NAME%') !== false ) $title = str_replace( '%PAGE_NAME%', get_the_title(), $title );
                $hide_inline = $toc_options['show_toc_in_widget_only'];

                $css_classes = '';
                // bullets?
                if ( $toc_options['bullet_spacing'] )
                    $css_classes .= ' have_bullets';
                else
                    $css_classes .= ' no_bullets';

                if ( $items ) {
                    // before widget (defined by themes)
                    echo $before_widget;

                    // display the widget title if one was input (before and after titles defined by themes)
                    if ( $title ) echo $before_title . $title . $after_title;

                    // display the list
                    echo '<ul class="toc_widget_list' . $css_classes . '">' . $items . '</ul>';

                    // after widget (defined by themes)
                    echo $after_widget;
                }

This are the full code of function extract_headings:

public function extract_headings( &$find, &$replace, $content = '' )
        {
            $matches = array();
            $anchor = '';
            $items = false;

            // reset the internal collision collection as the_content may have been triggered elsewhere
            // eg by themes or other plugins that need to read in content such as metadata fields in
            // the head html tag, or to provide descriptions to twitter/facebook
            $this->collision_collector = array();

            if ( is_array($find) && is_array($replace) && $content ) {
                // get all headings
                // the html spec allows for a maximum of 6 heading depths
                if ( preg_match_all('/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $content, $matches, PREG_SET_ORDER) ) {

                    // remove undesired headings (if any) as defined by heading_levels
                    if ( count($this->options['heading_levels']) != 6 ) {
                        $new_matches = array();
                        for ($i = 0; $i < count($matches); $i++) {
                            if ( in_array($matches[$i][2], $this->options['heading_levels']) )
                                $new_matches[] = $matches[$i];
                        }
                        $matches = $new_matches;
                    }

                    // remove specific headings if provided via the 'exclude' property
                    if ( $this->options['exclude'] ) {
                        $excluded_headings = explode('|', $this->options['exclude']);
                        if ( count($excluded_headings) > 0 ) {
                            for ($j = 0; $j < count($excluded_headings); $j++) {
                                // escape some regular expression characters
                                // others: http://www.php.net/manual/en/regexp.reference.meta.php
                                $excluded_headings[$j] = str_replace(
                                    array('*'), 
                                    array('.*'), 
                                    trim($excluded_headings[$j])
                                );
                            }

                            $new_matches = array();
                            for ($i = 0; $i < count($matches); $i++) {
                                $found = false;
                                for ($j = 0; $j < count($excluded_headings); $j++) {
                                    if ( @preg_match('/^' . $excluded_headings[$j] . '$/imU', strip_tags($matches[$i][0])) ) {
                                        $found = true;
                                        break;
                                    }
                                }
                                if (!$found) $new_matches[] = $matches[$i];
                            }
                            if ( count($matches) != count($new_matches) )
                                $matches = $new_matches;
                        }
                    }

                    // remove empty headings
                    $new_matches = array();
                    for ($i = 0; $i < count($matches); $i++) {
                        if ( trim( strip_tags($matches[$i][0]) ) != false )
                            $new_matches[] = $matches[$i];
                    }
                    if ( count($matches) != count($new_matches) )
                        $matches = $new_matches;

                    // check minimum number of headings
                    if ( count($matches) >= $this->options['start'] ) {

                        for ($i = 0; $i < count($matches); $i++) {
                            // get anchor and add to find and replace arrays
                            $anchor = $this->url_anchor_target( $matches[$i][0] );
                            $find[] = $matches[$i][0];
                            $replace[] = str_replace(
                                array(
                                    $matches[$i][1],    // start of heading                                 
                                    '</h' . $matches[$i][2] . '>'   // end of heading
                                ),
                                array(
                                    $matches[$i][1] . '<span id="' . $anchor . '">',
                                    '</span></h' . $matches[$i][2] . '>'
                                ),
                                $matches[$i][0]
                            );

                            // assemble flat list
                            if ( !$this->options['show_heirarchy'] ) {
                                $items .= '<li><a href="#' . $anchor . '">';
                                if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
                                $items .= strip_tags($matches[$i][0]) . '</a></li>';
                            }
                        }

                        // build a hierarchical toc?
                        // we could have tested for $items but that var can be quite large in some cases
                        if ( $this->options['show_heirarchy'] ) $items = $this->build_hierarchy( $matches );

                    }
                }
            }

            return $items;
        }

I tried it like this:

$items = '>'.$items
$tic = '>'.$tic
$find = '>'.$find
.
.
.

Unfortunately, nothing has hit the right place $ items addressed only the entire list The other Variables had no effect or led to errors

  • 写回答

1条回答 默认 最新

  • dongqiulei6805 2019-05-28 20:43
    关注

    The extract_headings is creating the list items. This section of the function...

    // assemble flat list
    if ( !$this->options['show_heirarchy'] ) {
        $items .= '<li><a href="#' . $anchor . '">';
        if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
        $items .= strip_tags($matches[$i][0]) . '</a></li>';
    }
    

    Should look like this:

    // assemble flat list
    if ( !$this->options['show_heirarchy'] ) {
        $items .= '<li><a href="#' . $anchor . '">>';
        if ( $this->options['ordered_list'] ) $items .= count($replace) . ' ';
        $items .= strip_tags($matches[$i][0]) . '</a></li>';
    }
    

    You can see I've added an extra > inside the hyperlink on the third line. If adding an extra > causes any issues, you can also use &gt;.

    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换