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

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.

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

PHP: Adding space to a string with lowercaseUppercase scenario including numbers

I have a string:
Baths: 2 full, 1 halfBuilt in 1990Views since listing: 1,845All time views: 2,00013 shoppers saved this homeHOA Fee: $399/moLaundry: In UnitParking: Carport, Garage - Detached, 2 spaces, 314 sqftLast sold: Apr 2011
I'm trying to make the above string more readable - so if you noticed there are no spaces in between new information. I have bold the above areas. What I noticed though is it ends with a smaller case letter then new information starts with an upper case.
Example: halfBuilt or 1,845All, etc. So my idea to make this readable is to add space in between them. So how do we do this in php and maybe regex? Thanks!
It seems like you want to break when letters/numbers run into an Uppercase letter. If that is this case this work:
/([a-z\d]+)([A-Z])/
Regex 101 demo: https://regex101.com/r/wV8eI6/1
PHP Usage:
$string = "Baths: 2 full, 1 **halfBuilt in 1990Views** since listing: **1,845All time views**: 2,00013 shoppers saved this **homeHOA Fee:** $399/moLaundry: **In UnitParking:** Carport, Garage - Detached, 2 spaces, **314 sqftLast** sold: Apr 2011";
$regex = '/([a-z\d]+)([A-Z])/';
echo preg_replace($regex, '$1 $2', $string);
Output:
Baths: 2 full, 1 half Built in 1990 Views since listing: 1,845 All time views: 2,00013 shoppers saved this home HOA Fee: $399/mo Laundry: In Unit Parking: Carport, Garage - Detached, 2 spaces, 314 sqft Last sold: Apr 2011
PHP Demo: https://eval.in/501264
This should do:
$string = preg_replace("/([a-z0-9])([A-Z])/", "$1 $2", $string);
echo $string;
// => "Baths: 2 full, 1 half Built in 1990 Views since listing: 1,845 All time views: 2,00013 shoppers saved this home HOA Fee: $399/mo Laundry: In Unit Parking: Carport, Garage - Detached, 2 spaces, 314 sqft Last sold: Apr 2011"

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;

Regular Expression - PHP

I am using following PHP code
<?
$data = file_get_contents('http://www.kitco.com/texten/texten.html');
preg_match_all('/([A-Z]{3,5}\s+[0-9]{1,2},[0-9]{4}\s+([0-9.NA]{2,10}\s+){1,7})/si',$data,$result);
$records = array();
foreach($result[1] as $date) {
$temp = preg_split('/\s+/',$date);
$index = array_shift($temp);
$index.= array_shift($temp);
$records[$index] = implode(',',$temp);
}
print_R($records);
?>
To READ the following data
--------------------------------------------------------------------------------
London Fix GOLD SILVER PLATINUM PALLADIUM
AM PM AM PM AM PM
--------------------------------------------------------------------------------
Jun 03,2013 1396.75 1402.50 22.4300 1466.00 1487.00 749.00 755.00
May 31,2013 1410.25 1394.50 22.5700 1471.00 1459.00 755.00 744.00
--------------------------------------------------------------------------------
What i want to do is Read GOLD ( BID & ASK ) price from below table, can anyone help in the regular expression changes?
New York Spot Price
MARKET IS CLOSED
Will open in
----------------------------------------------------------------------
Metals Bid Ask Change Low High
----------------------------------------------------------------------
Gold 1411.20 1412.20 +22.90 +1.65% 1390.10 1418.00
Silver 22.74 22.84 +0.48 +2.13% 22.26 23.08
Platinum 1495.00 1501.00 +41.00 +2.82% 1470.00 1511.00
Palladium 756.00 761.00 +7.00 +0.93% 750.00 766.00
----------------------------------------------------------------------
Last Update on Jun 03, 2013 at 17:14.58
----------------------------------------------------------------------
I'm not sure you could modify your existing regex to match both tables easily, but if you had the second table in a string, you could use:
$string = "PLAIN TEXT TABLE DATA HERE";
preg_match('/Gold\s+(\d+\.\d{2})\s+(\d+\.\d{2})/',$string,$matches);
$goldBid = $matches[1];
$goldAsk = $matches[2];
Here I'm only matching the numbers and period character. This code should return the numbers you're looking for. It uses your data string from your example.
<?
preg_match_all('!Gold\s+([0-9.]+)\s+([0-9.]+)!i',$data,$matches);
//New York
$ny_bid = $matches[1][0];
$ny_ask = $matches[2][0];
print("NY\nbid: $ny_bid\n");
print("ask: $ny_ask\n\n");
//Asia
$asia_bid = $matches[1][1];
$asia_ask = $matches[2][1];
print("Asia\nbid: $asia_bid\n");
print("ask: $asia_ask\n");
?>
Output
NY
bid: 1411.20
ask: 1412.20
Asia
bid: 1406.80
ask: 1407.80
You can also use T-Regx library
<?php
pattern('Gold\s+([0-9.]+)\s+([0-9.]+)', 'i')->match($data)->forEach(function ($m) {
print 'bid: ' . $m->group(1);
print 'ask: ' . $m->group(2);
});

Parsing a string after a certain string

I have a string ($source) which is containing the following data:
{"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson, David Thewlis, Benedict Cumberbatch","Plot":"Young Albert enlists to serve in World War I after his beloved horse is sold to the cavalry. Albert's hopeful journey takes him out of England and across Europe as the war rages on.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5MjgyNDY2NV5BMl5BanBnXkFtZTcwNjExNDc1Nw##._V1_SX640.jpg","imdbRating":"7.2","imdbVotes":"39,540","imdbID":"tt1568911","Response":"True"}
I'm extracting the title, the genre, the plot and so on by using this:
foreach(str_getcsv($source) as $item) {
list($k, $v) = explode(':', $item);
$$k = str_replace('"', '', $v);
}
So far, this works very well, I'm able to use $Title, $Genre and so on. The only thing that doesn't work is the URL to the poster since I'm exploding the ':' and the URL - of course - contains ':' (after the 'http').
How can I put the poster URL into a variable?
That looks like JSON data, why not simply:
$txt = '{"Title etc.....}';
$data = json_decode($txt);
$title = $data['Title'];
$genre = $data['Genre'];
etc...
variable variables are highly ugly, and you risk compromising your code by overwriting some other variable with the contents of the JSON data.
if you REALLY insist on poluting your namespace with auto-vivified variables, you can always use extract() to pull apart the array
Use json_decode
$str = '{"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson, David Thewlis, Benedict Cumberbatch","Plot":"Young Albert enlists to serve in World War I after his beloved horse is sold to the cavalry. Albert\'s hopeful journey takes him out of England and across Europe as the war rages on.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5MjgyNDY2NV5BMl5BanBnXkFtZTcwNjExNDc1Nw##._V1_SX640.jpg","imdbRating":"7.2","imdbVotes":"39,540","imdbID":"tt1568911","Response":"True"}';
$decode_string = json_decode($str);
print_r($decode_string);
echo $decode_string->Title;
Here is the running code Click Here
Its a json,
You should use json_decode
$str = '{"Title":"War Horse","Year":"2011","Rated":"PG-13","Released":"25 Dec 2011","Runtime":"2 h 26 min","Genre":"Drama, War","Director":"Steven Spielberg","Writer":"Lee Hall, Richard Curtis","Actors":"Jeremy Irvine, Emily Watson, David Thewlis, Benedict Cumberbatch","Plot":"Young Albert enlists to serve in World War I after his beloved horse is sold to the cavalry. Albert\'s hopeful journey takes him out of England and across Europe as the war rages on.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU5MjgyNDY2NV5BMl5BanBnXkFtZTcwNjExNDc1Nw##._V1_SX640.jpg","imdbRating":"7.2","imdbVotes":"39,540","imdbID":"tt1568911","Response":"True"}';
$arr = json_decode($str,true);
print_r($arr);
echo $arr['Title'];
echo $arr['Year'];
Notice, I have properly escaped the string.

Categories