Split PHP string from a specified word - php

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?

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
)

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}.*)$

$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];

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

you can use exploade function
http://in2.php.net/explode

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

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 ;)

You could us the explode function for this (http://nl1.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 :)

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 > > -- >',
)

Related

extract value from text file using php

I'm trying to extract the price 44,380.86 between date and # using preg_match_all() from the following line.One more thing is date Jan 1, 2015 will be dynamic.Can someone tell me how to complete it?
start on Jan 1, 2015 44,380.86 # of count: 15 tc
You can use this regex (regex explanation):
start on\s[A-Za-z]+\s[1-9]+,\s[0-9]+\s+(.*?)\s+#
Example Code:
<?php
preg_match_all(
"/start on\s[A-Za-z]+\s[1-9]+,\s[0-9]+\s+(.*?)\s+#/",
"start on Jan 1, 2015 44,380.86 # of count: 15 tc",
$matches
);
var_dump($matches);
I think this should be work for your problem with other changes:
preg_match_all("(\S+(?:\s\S+)*?)","Your string",$matches);
for your question you can use:
preg_match_all("(\S+(?:\s\S+)*?)","start on Jan 1, 2015 44,380.86 # of count: 15 tc",$matches);
echo $matches[5];
this regex parse your string with spaces, so when your string change, just you can edit index of $matches from 5 to what you want

file_get_contents() with newLine for Regex

I have a file called mail.txt with the following contents :
From: elvis#tabloid.org (The King)
Subject: be seein' ya around
Date: Mon, 23 Oct 2006 11:04:13
From: The Prez <president#whitehouse.gov>
Date: Wed, 25 Oct 2006 8:36:24
Subject: now, about your vote
I'm using Sublime Text in which the Regex ^\w+: works properly.
I'm using file_get_contents() to read the content from mail.txt and then use the same Regex for preg_replace() to highlight the output.
The issue is, when I use file_get_contents(), it doesn't consider \n and for that I tried nl2br(), but that didn't work either.
Below are the outputs in Sublime and PHP :
Sublime
PHP
Below is the PHP code :
<?php
$path = "./mail.txt";
if(!file_exists($path))
die("File does not exist");
else {
if(!($handle = fopen($path, "r")))
die("File could not be opened");
else {
$file_data = file_get_contents($path);
}
}
$mod_file = preg_replace("/^\w+:/", "<span class='replaced'>$0</span>", $file_data);
echo "<pre>".$mod_file."</pre>";
?>
How to solve this issue?
You need to use m or Multiline flag.See demo.
https://regex101.com/r/cT0hV4/12
$re = "/^\\w+:/m";
$str = "From: elvis#tabloid.org (The King)\nSubject: be seein' ya around\nDate: Mon, 23 Oct 2006 11:04:13\nFrom: The Prez <president#whitehouse.gov>\nDate: Wed, 25 Oct 2006 8:36:24\nSubject: now, about your vote";
preg_match_all($re, $str, $matches);

How to find the position of the first occurrence of a pattern using PHP

I am trying to figure out a way to parse an email.
I am stuck trying to figure out how to search for the first occurrence of a text that is in this format
> On Mar 12, 2015, at 7:47 AM, Mike G <email#yourdomain.com> wrote:
the text will start with > On and ends with wrote:
On Mar 12, 2015, at 7:47 AM, Mike G wrote:
How can I find that in PHP?
I could do
$msg = strpos($msg, '> On'); // to get the first position
$msg = strstr($msg, '> On', true); // with PHP 5.3+ to get the text prior the first '> On '
But I need to look for a similar pattern line to be more acurate.
I tried this code:
$matches = '';
$pattern = "/ On*<[a-zA-Z0-9._-]#[a-zA-Z0-9._-]> wrote:/";
preg_match($pattern, $msg, $matches);
$msg = strstr($msg, $matches, true);
But I am not finding any results in the text.
I think this should do it. If the whitespace is optional change the s+ to s*.
preg_match('~>\s+.*?<([^>]*)>\s+wrote:~', '> On Mar 12, 2015, at 7:47 AM, Mike G <email#yourdomain.com> wrote:', $email);
echo $email[1];
If you want to be safer and require the 'On' as well...
preg_match('~>\s+On.*?<([^>]*)>\s+wrote:~', '> On Mar 12, 2015, at 7:47 AM, Mike G <email#yourdomain.com> wrote:', $email);

How to Remove Bulk Dynamic Date Format in PHP?

i have sentences this, and i want every sentence which found by date format will be removed
$mysent = 'Jul 2, 2014 . I went to special place. Aug 30, 2015 . We went to Paris.';
I try use this array :
$sasi = array('Jan ','Feb ','Mar ','Apr ','May ','Jun ','Jul ','Aug ','Sep ','Oct ','Nov ','Dec ');
$angka = range(1,2015);
$bulan = $sasi.$angka.", ".$angka;
$contoh = str_replace($bulan,'',$contoh);
echo $contoh;
but the date format didn't removed, help me, thank
You can use Regular Expressions:
$string = 'Jul 2, 2014 . I went to special place. Aug 30, 2015 . We went to Paris.';
$result = preg_replace('/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d+,\s\d{4}\s\.\s/i', '', $string);
echo $result;
Outputs:
I went to special place. We went to Paris.
PHPFiddle Link: http://www.phpfiddle.org/main/code/h82y-ucku

cut string to get linux time

I have this text:
DESCRIPTION:When: ‎יום שלישי 28 אוקטובר 2014 10:00 עד 11
I would like to cut the text in order to convert the dat to linux time date
how do I get the 28 (day) , אוקטובר (month), and 2014 (year), 10:00 (time) with regular expression ?
Is there any better way?
you can try to extract all numeric value using preg_match then concat as per your need.
preg_match_all('!\d+!', $yourString, $matches);
echo $dateTime = $matches[0][2].':'.$matches[0][3].' '.$matches[0][0].'-'.$matches[0][4].'-'.$matches[0][1];
echo '<pre>';
print_r($matches[0]);
exit;

Categories