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

时间:2019-05-28 18:24:05

标签: php string variables echo

我想在目录代码中做些改动。

我想在每个标题前面添加一个符号。该字符应被识别为文本。

我已经尝试了一些方法,但是不幸的是我没有找到合适的变量。

代码来自Wordpress的插件

我已经尝试了以下变量:

$items
$tic
$find
$replace
$post

以下是打印列表的代码:

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;
                }

这是功能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;
        }

我这样尝试过:

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

不幸的是,没有任何事情来对地方了 $个项目仅解决了整个列表 其他变量无效或导致错误

1 个答案:

答案 0 :(得分:0)

extract_headings正在创建列表项。此部分功能...

// 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>';
}

应如下所示:

// 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>';
}

您可以看到我在第三行的超链接中添加了一个额外的>。如果添加额外的>会导致任何问题,则也可以使用&gt;