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"
Related
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
Having difficuties matching a date in the given string. Tried a myriad of regex suggestions. Keep on getting "No date found", while the date is obviously there: 07/02/2016.
What am I missing?
function matchDate($str) {
if (preg_match('/\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\b/', $str, $mDdate)) {
return $mDdate[0];
} else {
return "No date found.";
}
}
$str = "FISH HOUSE KINGS FISH HOUSE 100 W Broadway Long Beach, Ca. 90802 562-432-7463 Server: Ezbeth 07/02/2016 Table 44/1 8:38 PM 60018 10.00 18.75 enon Drop Fried atiish D I obster Crunchy Roll callap and Shrimp Char D 13.50 22.50 6.95 101.60 Cheeseburger 1/2lb D (2 14.95) 29.90 Caesar Salad Subtotal Tax 9.14 110.74 110.74 Total Ba 1ance Due KING'S FISH HOUSE Welcome To The House That Seafood Built Find Us Online #KingsFishHouse ";
echo matchDate($str);
For your given example, this is
\b\d{2}/\d{2}/\d{4}\b
See a demo on regex101.com.
The problem with your example regex is that you need to escape the / character in the pattern.
If you don't escape the / character, regex will understand it as the end of the regex pattern.
Based on your example, the solution should be:
\b(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/(19|20)?[0-9]{2}\b
You can see it in action in this demo
this is my string:in variable $raw
Movie Title: Ittefaq (2017) Director: Abhay Chopra Stars: Sonakshi
Sinha, Sidharth Malhotra, Akshaye Khanna Release Date: 3 November 2017
(India) Genres: Mystery, Thriller Format: MatRoska (Mkv) File Size:
850MB Resolution: 1280x536 Runtime: 01:45:31 Language: Hindi
Subtitles: English [Softcoded] - Muxed Encoder: MkvCage (MC) Team
Source: Hon3y /
and now i want to extract "850MB" from it.
Note: the whole string is changed every time.
but keywords like "File Size:", "resolution" remain same but their value changes.
i only want to extract value of file size from it.
You get all possible values, you could use :
if (preg_match('~File Size: ([^\s]+)~', $raw, $matches)) {
echo $matches[1];
}
Will get the string from File Size: until the next whitespace character.
I think the following pattern will be tight enough to only capture what you need and not by mistake get something else in the text.
/Size: ([\d\.MGTB]+)/
It will accept digits, dots and MB, GB, TB filesizes.
(If filesizes are with dots, ex. 1.35 GB)
preg_match("/Size: ([\d\.MGTB]+)/", $raw, $match);
$raw = 'Movie Title: Ittefaq (2017) Director: Abhay Chopra Stars: Sonakshi Sinha, Sidharth Malhotra, Akshaye Khanna Release Date: 3 November 2017 (India) Genres: Mystery, Thriller Format: MatRoska (Mkv) File Size: 850MB Resolution: 1280x536 Runtime: 01:45:31 Language: Hindi Subtitles: English [Softcoded] - Muxed Encoder: MkvCage (MC) Team Source: Hon3y /';
$matches = [];
preg_match("/Size: ([\da-zA-Z]+)/", $raw, $matches);
echo $matches[1];
The example above will output:
850MB
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.
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.