-1

I am trying to get a string of words located between the words "Free" and "LESSON", which works just fine when I have a string that consists of one text block with one instance of "Free" and "LESSON" only. The challenge I am facing is how to achieve this when there are a multiple, line break separated text blocks in one string?

<?php

$value = '
Sam Mutimer
3m 8s
Social Media credit
Free
How to find your social media audience
LESSON
How to find your social media audience

Sam Mutimer
3m 20s
Social Media credit
Free
Defining your social media personality
LESSON
Defining your social media personality

Sam Mutimer
2m 5s
Social Media credit
Free
Dealing with complaints on social media
LESSON
Dealing with complaints on social media';

$value = strstr($value, "Free", true); //gets all text from needle on
$value = strstr($value, "LESSON", true); //gets all text before needle
echo $value;

?>

My best guess is to put the string into an array, but how does one separate the existing string into multiple substrings based on a line break?

Appreciate any help, thank you.

Armitage2k
  • 1,164
  • 2
  • 27
  • 59
  • Please check reference questions for your problem on site first and if they didn't answer your specific question (e.g. because you could not split it up into two common problems that have been answered by four people each since 2008), then create a new question and provide reference and a reproducible example. – hakre Nov 02 '21 at 14:12

1 Answers1

3

You can use regex:

preg_match_all("|Free(.*?)Lesson|si", $value, $out);
var_dump($out[1]);
Pavel Třupek
  • 898
  • 6
  • 19
  • 1
    It may appear likely but it must not be that CRLF is the line terminator. You may want to consider \R to make the regex more portable or any of the other options. See here: https://stackoverflow.com/q/18988536/367456 – hakre Nov 02 '21 at 14:16
  • @hakre It wasn't in my original post, but was edited by point-hunter Mihai Matei – Pavel Třupek Nov 03 '21 at 07:05