简体   繁体   中英

Parse string based off string search and reconstruct

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM