cut string to get linux time - php

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;

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

Split string by full stop in php exclude "a.m."

I am using the preg_split function in PHP to split a paragraph to be several sentences.
In my case:
$str = 'Applicants can check the final result of Admissions through the online enquiry system. The online enquiry system will be available from 10:00 a.m. on November 16 (Wednesday).';
$arr = preg_split('/\./', $str);
How can I exclude the case when there is an a.m. or p.m.?
You should be able to use (*SKIP)(*FAIL) to block the am/pm matches. You can read more about the approach here, http://www.rexegg.com/regex-best-trick.html.
[ap]\.m\.(*SKIP)(*FAIL)|\.
Regex Demo: https://regex101.com/r/uD9xD7/1
Demo: https://eval.in/548705
PHP Usage:
$str = 'Applicants can check the final result of Admissions through the online enquiry system. The online enquiry system will be available from 10:00 a.m. on November 16 (Wednesday).';
$arr = preg_split('/[ap]\.m\.(*SKIP)(*FAIL)|\./', $str);
print_r($arr);
Output:
Array
(
[0] => Applicants can check the final result of Admissions through the online enquiry system
[1] => The online enquiry system will be available from 10:00 a.m. on November 16 (Wednesday)
[2] =>
)
If A.M. should also be allowed use the i modifier.

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

String padding for a text template

I'm creating a PDF file from a txt-template with tcpdf ([Example 8][1]). The txt-template looks like this:
SALUTATION
FIRSTNAME LASTNAME
STREET CURRENTDATE
SOMEMOREINFORMATION MYWEBSITE
I replace those markers with the correct value. So that it would look like this:
Mr.
John Doe
Downingstreet 10 14th May, 2010
john#doe.com www.stackoverflow.com
In this example, when I replace the values, the indention of the date is dependent on the length of the street name (which I don't want). I could solve this issue with str_pad but the problem is, I normally use three columns and there are lines which only have content in col1 and col3 as in the last line. How can I solve that problem? Is there something like the "overwrite" function in Word, that when you write, the text just gets overwritten?
Thanks in advance.
Count street's string length and then add/remove left padding of date.
You can use sprintf, e.g.
function something($street, $currentDate, $foo) {
$s = sprintf('%-20s %-18s %s',
$street,
$currentDate,
$foo
);
return $s;
}
echo something('streetA', '14th May, 2010', 'lalala'), "\n";
echo something('Downingstreet 10', '14th May, 2010', 'lalala'), "\n";
echo something('abcdefghijklmnopqrstuvwxyz 10', '14th May, 2010', 'lalala'), "\n";
prints
streetA 14th May, 2010 lalala
Downingstreet 10 14th May, 2010 lalala
abcdefghijklmnopqrstuvwxyz 10 14th May, 2010 lalala
(as you can see from the third line the width specification is the minimum length, so you might have to use something like substr())
I presume you are just str_replace()'ing the placeholders with their values?
$streetPlaceHolder = 'STREET ';
$streetReplacement = str_pad('Downingstreet 10', strlen($streetPlaceHolder));
$template = str_replace($streetPlaceHolder, $streetReplacement, $template);
Presumably you will run into the same problem with SOMEMOREINFORMATION. This same solution can be used.
I realize you said str_pad was not an ideal solution for you. However, I do not understand why, even if you extend this to three columns. You can still get by with this method.

Categories