0

I have this php string

$mystring ="Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >"

I want to split the string starting from "On Fri, Mar 21, 2014".How do I achieve this?

Note - the spilt condition can be general. i.e it can also be 'On Sat, Mar 22' or 'On Wed, Mar 29' etc

Also mention which php function should I use?

1
  • What is your exact desired output from your input string? Should "On Fri, Mar 21, 2014" be consumed in the explosion or should it remain in the output array? Commented Mar 21, 2022 at 11:23

9 Answers 9

2

Because it would not work very good if you just split at the "On" word (could also exist in the text before, which I assume may be different), I suggest the following possibility:

$str = "Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >"; if (preg_match('/^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)$/', $str, $matches)) { print_r($matches); }

This gives you an output like the following, which should include all necessary values. Feel free to add an "i" after the second slash in the preg_match regex for case insensitive.

Array ( [0] => Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- > [1] => Yes YEs I am answering! [2] => On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- > [3] => Fri )

Sign up to request clarification or add additional context in comments.

4 Comments

How do I compare the month and year for that, in general format?
can 'On Fri, Mar 21, 2014' be generalized ?
If you wish to extract the date and time into separate variables of the $matches array, you may extend the RegExp to the following (thanks Bjarke): /^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\w{3}) (\d{2}), (\d{4}) at (\d:\d{2}) (AM|PM).*)$/
If that doesn't answer your question, please specify what you mean by "generalized"
1

I would suggest a regex in order to do this, dieBeiden basically has it, though I would modify his regex a bit:

^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun), \w{3} \d{2}.*)$

Comments

0

$arr = explode('Yes YEs I am answering! ', $string);

It will find that word, delete it, and split on that place array!

And then you get On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >"

next explode on ','

$arr = explde(',',$arr); $strings = $arr[0].','.$arr[1];

Comments

0

Check this,this example I did found

<?php
$whois = "Record last updated on 10-Apr-2011.Record expires on 08-Oct-2012.Record Expires on 08-Oct-2008.";
$expires = preg_split('/Expires|expires/', $whois);
array_shift($expires);
echo "<pre>";
print_r($expires);
?>

gives

Array
(
    [0] =>  on 08-Oct-2012.Record 
    [1] =>  on 08-Oct-2008.
)

You can also this

http://board.phpbuilder.com/showthread.php?10384775-RESOLVED-Split-String-at-first-word-match

Comments

0

you can use exploade function

https://www.php.net/explode

1 Comment

Explode will not work because its needs to be generalised to a format of string.
0

Try with explode() like

$tempArr1 = explode('!' , $mystring);
$tempArr2 = explode(',' , $tempArr1);
echo $tempArr2[0].', '.$tempArr[1].', '.$tempArr[2];

Comments

0

If the initial part the string is ALWAYS "Yes YEs I am answering! " you can delete the first 24 characters from the string with the function

$mystring2 = substr($mystring, 24);

Also take a look at the explode() function ;)

Comments

0

You could us the explode function for this (https://www.php.net/explode) and split it on the word 'On', but when 'On' also occurs in the string, you're in trouble.

A better idea would be to use a regular expression with preg_split (http://www.php.net/manual/en/function.preg-split.php), with something like this:

<?php
$mystring ="Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >";

$splitted = preg_split('/On ..., ... [0-9]{2}, [0-9]{4} at [0-9]:[0-9]{2} (AM|PM)/', $mystring);

var_dump($splitted);


?>

Feel free to make the regular expression more sophisticated :)

Comments

0

If you want to retain the datetime substring, then you can consume the leading space, then lookahead for as much of the datetime substring as you like.

Code: (Demo)

$mystring ="Yes YEs I am answering! On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >";

var_export(
    preg_split('/ (?=On (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun))/', $mystring)
);

Output:

array (
  0 => 'Yes YEs I am answering!',
  1 => 'On Fri, Mar 21, 2014 at 2:49 PM, Ajey Charantimath wrote: > answer to this question > > -- >',
)

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.