Home > database >  Search Paragraph and return entire first line until and including entire last line using string keyw
Search Paragraph and return entire first line until and including entire last line using string keyw

Time:01-28

I'm trying to find a way to isolate a specific paragraph using a string as a starting point, where the string could be a word in any part of the line (not necessarily the end or the beginning).

So it will grab that entire line where the string occurs, and then it will grab until the line where it finds the secondary string. I've checked various questions and I'm not finding quite what I want. Here's an example input paragraph with the desired output paragraph:

Input:

JUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
ABC
DEF GHI JKL
MNO PQR STW
UVW XYZ
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT

Objective: I want to get everything from ABC until XYZ. ABC and XYZ will always only have one occurence in the paragraph - and ABC will always occur before XYZ. My paragraphs in questions are being obtained from emails, and I'm currently using PhpMimeMailParser to parse the email.

Desired Output:

ABC
DEF GHI JKL
MNO PQR STW
UVW XYZ

CodePudding user response:

startWord = "ABC"
endWord = "XYZ"
result = ""
foreach(word in para)
{
    if(word == startWord || result.Length > 0)
      result  = word;
    if(word == endWord)
      break;
}
return result;

-- if there are multiple occurrences of the sequence then repeat above logic.

CodePudding user response:


$data = "
JUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
ABC
DEF GHI JKL
MNO PQR STW
UVW XYZ
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
";

$start = 'ABC';
$end = 'XYZ';

$startIndex = strpos($data, $start);
$endIndex = strpos($data, $end, $startIndex)   strlen($end);

$result = substr($data, $startIndex, $endIndex - $startIndex);

echo $result;

For case-insensitive search use stripos() instead of strpos().

CodePudding user response:

$data = "
JUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
ABC
DEF GHI JKL
MNO PQR STW
UVW XYZ
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
";

$result = preg_replace('/(.*)(ABC.*XYZ)(.*)/s', '\2', $data);

echo $result;

For case-insensitivity change add the regex modifier i after the pattern:

'/(.*)(ABC.*XYZ)(.*)/si'

CodePudding user response:

You can use this:

$data = "
JUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
ABC
DEF GHI JKL
MNO PQR STW
UVW XYZ
JUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
JUNKTEXTJUNKTEXT
";

echo substr($data, strpos($data, "ABC"), strpos($data, "XYZ")-62 strlen("XYZ"));

You need to get data from string from index A to index B.

strpos($data, "ABC") is index of "ABC" string.
strpos($data, "XYZ")-62 strlen("XYZ") is LENGHT of the string you want to take. To get this lenght you need from strpos($data, "XYZ") result minus first result and add lenght of second searched string. Why? Because strpos() return index of start of searched value. To take end you must add

  •  Tags:  
  • Related