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
Related
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"
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);
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;
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 > > -- >',
)
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.