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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名