douchuitang0331 2018-07-17 16:06
浏览 13
已采纳

解析基于字符串搜索和重构的字符串

I am trying to reconstruct a string based off defined elements found it it. Each element that is found, I need to get the text related to it until the next heading is found. Below I have added some code to help explain better. Any direction/help is appreciated.

//defined headings to search on
$search_items ='Summary:,Education:,Experience:,Other:,Qualifications/Requirements:';

$string = 'Summary: It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Education:Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like.Other:Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.';

$headers = explode(',', $search_items);

foreach ($headers as $heading) {
    $substring = substr($string, strpos(utf8_encode($string), utf8_encode($heading)) + 1);   
    if ($substring  !== false) {
       echo "<p><strong>$heading</strong></p><p></p>";
    }
} 

With the code above, I can find all the headings and wrap them in the strong tag. Then $substring contains all the remaining of the text after the heading. The problem is that I only want the text until the next heading is found so I can drop it in the empty p tags.

so using the string above it would look something like:

Summary:

It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Education:

Various versions have evolved over the years, sometimes by accident, sometimes on purpose injected humour and the like.

  • 写回答

1条回答 默认 最新

  • dstjh46606 2018-07-17 16:30
    关注

    First of all, the way you store your texts isn't very good. What happens if the actual text contains one of the headings as part of it? That said, the easiest way to do this is actually to split your string by regular expression and get headings and string data. Here's a sample code:

    <?php
    
    $search_items ='Summary:,Education:,Experience:,Other:,Qualifications/Requirements:';
    
    $string = 'Summary: It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Education:Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like.Other:Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.';
    
    $headers = explode(',', $search_items);
    $pattern = '#(' . implode('|', array_map(function($a) { return preg_quote($a); }, $headers)) . ')#';
    
    $data = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    
    for ($i=0; $i<count($data); $i+=2) {
        echo "<p><strong>{$data[$i]}</strong></p><p>{$data[$i+1]}</p>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题