0

I want extract several strings of X length that start with specific string ex: 'Peter was [and then can be anything]' from a large string into an array like this

$myString = "Today Peter was in the zoo doin son and so on, 
    yesterday Peter was with Karen eating so on and so on, 
    the day before yesterday Peter was dead jesus christ AND THE STRING KEEP GOING";

$theResult = [
    'Peter was in the zoo....',
    'Peter was with Karen....',
    'Peter was dead....',
    etc...
]

The clue string is Peter was, and then the rest can be anything many times

6
  • 2
    Can you please include your best attempt and explain where and how it fails? Also, giving an example of an input value and expected output for that value would make your case much clearer. Commented Jan 26, 2021 at 18:03
  • Few realistic examples needed and your ideas. Commented Jan 26, 2021 at 18:17
  • Thanks for yours answers, I have edited trying to be more clear, I hope is enough. Commented Jan 26, 2021 at 18:30
  • But what is the end limit of extraction? Is it comma or end of the sentence? Commented Jan 26, 2021 at 18:36
  • I have answered both your questions. Commented Jan 26, 2021 at 18:41

3 Answers 3

1

The answer after you changed the question:

$theResult = preg_split('/(?=Peter was)/', $myString);

Will give:

array(
  0 => Today
  1 => Peter was in the zoo doin son and so on, yesterday
  2 => Peter was with Karen eating so on and so on, the day before yesterday
  3 => Peter was dead jesus christ AND THE STRING KEEP GOING
)
Sign up to request clarification or add additional context in comments.

2 Comments

Great, but I don't need the word "Today" in it, and the string is about 20Mb large, and "Peter was" will exists between 4-20 times, and I need limit the length of the string, seems effective, I going to try it anyway
It works better thank expected, thank you, it ends like this foreach (preg_split ('/(?=' . $spliter . ')/', $wholeText) as $text) { if (str_starts_with ($text, $spliter)) { $myWords[] = substr ($text, 0, 50); } }
0

This is the answer to the question you had before you changed it...

$myString = 'We are writing a string here and abc taking this string xcv we are going to extract ikw some text from it and put it in an array';

preg_match('/.*(abc.{10}).*(xcv.{10}).*(ikw.{10})/', $myString, $theResult);

Will put 4 items in $theResult. The 0th group which is all of them and then 1, 2, and 3 for the captures you want.

array(
  0 => We are writing a string here and abc taking this string xcv we are going to extract ikw some text
  1 => abc taking th
  2 => xcv we are go
  3 => ikw some text
)

Comments

0

You can use explode function to convert string to array like below

$myString = "Today Peter was in the zoo doin son and so on, 
    yesterday Peter was with Karen eating so on and so on, 
    the day before yesterday Peter was dead jesus christ AND THE STRING KEEP GOING";
   $exp=explode('Peter was',$myString);
   print_r($exp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.