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
Related
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
I am trying to show temperature on my website. I have a forecast in json that I get from api and there is this field that I need to get the temperature from:
"fcttext_metric":"A mainly sunny sky. High near 30 ° C. Winds SE at 10 to 15 km/h."
I tried to parse only float numbers which works well, but it fails when there is more text behind it like in the case above. I tried to show only first 2 chars but when the temperature is negative it fails again. What can I do about this?
$str = 'A mainly sunny sky. High near 30 ° C. Winds SE at 10 to 15 km/h.';
$deg = filter_var($str, FILTER_SANITIZE_NUMBER_FLOAT);
$res = substr($deg, 0, 2);
print_r($res);
I would use this regex. It should match integer values before ° (°) sign. Like 30 or -30
preg_match('/(\-?\d+)\s*°/', $str, $matches);
echo $matches[1];
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 have a file name like this
Birdman.2014.DVDSCR.X264-PLAYNOW.mkv
Divergent (2014) 720p Blu-Ray x2649 [Dual-Audio][English DD 5.1 +
Hindi DD 5.1] - Mafiaking - TeamTNT ExClusive.mkv
Cradle 2 The Grave 2003 HDTVRip 720p Dual-Audio[Eng+Hindi] ~ BRAR
I have to seperated movie name and year
For getting year in use perg_match like
$s="Divergent (2014) 720p Blu-Ray x2649 [Dual-Audio][English DD 5.1 + Hindi DD 5.1] - Mafiaking - TeamTNT ExClusive.mkv";
if (preg_match('/\b\d{4}\b/', $s, $matches)) {
$year = $matches[0];
}
echo $year;
result :- 2014 //working perfect
But I also want to get movie name like
Divergent
Birdman
Cradle 2 The Grave
I also check with this link
Extracting movie name and year from string were year is optional
But nothing happen
How i get it? Please help or provide hint to achive it.
^.+?(?=\s*[(.]?(\d{4}))
You can try this.The movie name will be in match and year in the first group.See demo.
https://regex101.com/r/vA0yQ3/2
$re = "/^.+?(?=\\s*[(.]?(\\d{4}))/mi";
$str = "\n\nBirdman.2014.DVDSCR.X264-PLAYNOW.mkv\n\nDivergent (2014) 720p Blu-Ray x2649 [Dual-Audio][English DD 5.1 + Hindi DD 5.1] - Mafiaking - TeamTNT ExClusive.mkv\n\nCradle 2 The Grave 2003 HDTVRip 720p Dual-Audio[Eng+Hindi] ~ BRAR\n";
preg_match_all($re, $str, $matches);
This is the content of one mysql table field:
Flash LEDs: 0.5W
LED lamps: 5mm
Low Powers: 0.06W, 0.2W
Remarks(1): this is remark1
----------
Accessories: Light Engine
Lifestyle Lights: Ambion, Crane Fun
Office Lights: OL-Deluxe Series
Street Lights: Dolphin
Retrofits: SL-10A, SL-60A
Remarks(2): this is remark2
----------
Infrared Receiver Module: High Data Rate Short Burst
Optical Sensors: Ambient Light Sensor, Proximity Sensor, RGB Color Sensor
Photo Coupler: Transistor
Remarks(3): this is remark3
----------
Display: Dot Matrix
Remarks(4): this is remark4
Now, I want to read the remarks and store them in a variable. Remarks(1), Remarks(2), etc. are fixed. 'this is remark1', etc. come from form input fields, so they are flexible.
Basically what I need is: Read everything between 'Remarks(1):' and '--------' and save it in a variable.
Thanks for your help.
You can use regex:
preg_match_all("~Remarks\(([^)]+)\):([^\n]+)~", $str, $m);
As seen on ideone.
The regex will put X in match group 1, Y in match group 2 (Remarks(X): Y)
This would be a job for regular expressions, which allow you to match on exactly the kinds of rules your requirements express. Here is a tutorial for you.
Use preg function for this or otherwise you can explode and implode function to get correct result. Don't Use Substring it may not provide correction.
Example of Implode and Explode Function for your query string :
$sdr = "Remarks(4): this is remark4";
$sdr1 = explode(":",$sdr);
$frst = $sdr1[0];
$sdr2 = array_shift($sdr1);
$secnd = implode(" ", $sdr1);
echo "First String - ".$frst;
echo "<br>";
echo "Second String - ".$secnd;
echo "<br>";
Your Answer :
First String - Remarks(4)
Second String - this is remark4