dongpiansui8755 2019-05-29 03:18
浏览 62

通过PHP搜索和解析HTML自定义标签

I've created a set of custom tags that go inside their own wrapper (see example below & there can be many different tags on a page at once) that I'm trying to find the best method for getting the tags and their info, and easiest for the server to process.

All the content I've been able to find on this is dated a few years at least and I figured maybe someone in the community would have ideas to help point me in a better direction.

[sl_heading tag='h3' padding='10' heading='Our Most Recent Posts' color='' style='' size='' subheading_active='' subheading_size='15' custom_class=''][/sl_heading ]

[sl_posts posts_type='general' categories='24' link='category' posts_style='posts-grid' columns='4' contents='title_read_more' content_length='excerpt_read_more' preview_mode='auto' image_size='thumbnail' items='-1' offset='0' paginate='no']

So basically what I'm trying to do is the editor saves this to the database, and I'm trying to efficiently and quickly parse this out to the proper code on the website side. So my method is detecting the tag, determine what it is and what the tag values are, and show the proper HTML code in return.

I've already got everything but the parsing out the code part done, and I'm stuck on how to properly search and find the tags and their values effectively.

PS. Any help is appreciated, and I've searched through a lot of SO posts so what I'm trying to do hasn't been asked about in years (outdated posts) for what I can only understandably assume were much earlier versions of PHP.

  • 写回答

1条回答 默认 最新

  • doujing6053 2019-05-30 01:43
    关注

    So I took a lot of time out and studied a few different systems that are open source, and figured out an effective, and fast working solution. I took a simple function I seen and reworked it to work for me. This finds all instances of substring text I look for (something like [start] and [end] ) and gets all the content between it for me. It then replaces that entire string and replaces it with the appropriate content.

    See code below, copy paste into PHPfiddle.org for quick run.

    <?php
    
    function getContents($str, $startDelimiter, $endDelimiter) {
        $contents = array();
        $startDelimiterLength = strlen($startDelimiter);
        $endDelimiterLength = strlen($endDelimiter);
        $startFrom = 0;
        $contentStart = 0;
        $contentEnd = 0;
        while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
            $contentStart += $startDelimiterLength;
            $contentEnd = strpos($str, $endDelimiter, $contentStart);
            if (false === $contentEnd) { break; }
    
            $content_length = $contentEnd - $contentStart;
            $the_content = substr($str, $contentStart, $content_length);
            $replace_this = "$startDelimiter$the_content$endDelimiter";
            $contents[] = array($the_content, $replace_this);
            $startFrom = $contentEnd + $endDelimiterLength;
      }
    
      return $contents;
    }
    
    $thiss = "This is a test text area, filled with dummy text such as: Lorem ipsum dolor sit amet. [advertisers-display]123[/advertisers-display] This is a test text area, filled with dummy text such as: Lorem ipsum dolor sit amet. [advertisers-display]456[/advertisers-display] This is a test text area, filled with dummy text such as: Lorem ipsum dolor sit amet. [advertisers-display]789[/advertisers-display]";
    
    $tag_start = "[advertisers-display]";
    $tag_end = "[/advertisers-display]";
    
    $this_array_get = getContents($thiss, $tag_start, $tag_end);
    foreach($this_array_get as $this_get) {
        if(isset($this_get[1])) {
            echo "ID $this_get[0] | REPLACE THIS -> $this_get[1] WITH THIS -> [ REPLACED $this_get[0] ]<br />";
            $thiss = str_replace($this_get[1], "[ REPLACED $this_get[0] ]", $thiss, $num);
        }
    }
    echo "<br /> $thiss";
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致