Regex PHP, Find characters in specific position - php

I explain my problem : I'm working on different kind of address
" 25 Down Street 15000 London "
" 25 B Down Street 15000 London "
" Building A 25 Down Street 15000 London "
I found a way to determine which is the number of the street on all case with this regex :
`^([1-9][0-9]{0,2}(?:\s*[A-Z])?)\b`
But now i got a problem that i can't solve, i need when the case is real to determine characters which are before the street's number .
Example : " Building 2 25 Down Street 15000 London " i need here to find only "Building 2"
I understand that i have to find characters before the first number of this string.
Keep searching on my own but will be great if someone got a solution for me .
Thank you .
Edit my code now is :
preg_match('/^(.*?)\d+\s+\D+/', $cleanAdressNode, $result, PREG_OFFSET_CAPTURE,0);
print $result[0][0];
return $result[0][0];
and the result now is : Résidence Les Thermes 1 15 boulevard Jean Jaurès instead of only : Résidence Les Thermes 1

How about:
preg_match('/^(\D*)/', $str, $match);
You will find in $match[1] everything that is not a digit at the begining of the string.
According to your example:
preg_match('/^(.*?)\d+\s+\D+/', $str, $match);

If you only want to match the first non-numeric characters, ^([^0-9]*) should do the trick. It uses class negation to grab every non-numeric characters at the start of the string.

Related

Using preg_replace to reformat money amounts in text with PHP

I'm struggling with some regular expressions. What I want to do is find money amounts in a string, remove the €,$, or £ but keep the number, and then look to see if there is a 'b' or an 'm' - in which case write 'million platinum coins' or 'million gold coin' respectively but otherwise just put 'gold coins'.
I have most of that as a hack (see below) with the small problem that my regex does not seem to work. The money amount comes out unchanged.
Desired behaviour examples
I intend to leave the decimal places and thousands separators as is
$12.6m ==> 12.6 million gold coins
£2b ==> 2 million platinum coins
€99 ==> 99 gold coins
My code
Here is my non-working code (I suspect my regex might be wrong).
protected function funnymoney($text){
$text = preg_replace('/[€$£]*([0-9\.,]+)([mb])/i','\0 %\1%',$text);
$text = str_replace('%b%','million platnum coins',$text);
$text = str_replace('%m%','million gold coins',$text);
$text = str_replace('%%','gold coins',$text);
return $text;
}
I would greatly appreciate it if someone could explain to me what I am missing or getting wrong and guide me to the right answer. You may safely assume I know very little about regular expressions. I would like to understand why the solution works too if I can.
Using preg_replace_callback, you can do this in a single function call:
define ("re", '/[€$£]*(\.\d+|\d+(?:[.,]\d+)?)([mb]|)/i');
function funnymoney($text) {
return preg_replace_callback(re, function($m) {
return $m[1] .
($m[2] != "" ? " million" : "") . ($m[2] == "b" ? " platinum" : " gold") .
" coins";
}, $text);
}
// not call this function
echo funnymoney('$12.6m');
//=> "12.6 million gold coins"
echo funnymoney('£2b');
//=> "2 million platinum coins"
echo funnymoney('€99');
//=> "99 gold coins"
I am not sure how you intend to handle decimal places and thousands separators, so that part of my pattern may require adjustment. Beyond that, match the leading currency symbol (so that it is consumed/removed, then capture the numeric substring, then capture the optional trailing units (b or m).
Use a lookup array to translate the units to English. When the unit character is missing, apply the fallback value from the lookup array.
A lookup array will make your task easier to read and maintain.
Code: (Demo)
$str = '$1.1m
Foo
£2,2b
Bar
€99.9';
$lookup = [
'b' => 'million platinum coins',
'm' => 'million gold coins',
'' => 'gold coins',
];
echo preg_replace_callback(
'~[$£€](\d+(?:[.,]\d+)?)([bm]?)~iu',
function($m) use ($lookup) {
return "$m[1] " . $lookup[strtolower($m[2])];
},
$str
);
Output:
1.1 million gold coins
Foo
2,2 million platinum coins
Bar
99.9 gold coins
Your regex has a first full match on the string, and it goes on index 0 of the returning array, but it seems you just need the capturing groups.
$text = preg_replace('/[€$£]*([0-9\.,]+)([mb])/i','\1 %\2%',$text);
Funny question, btw!
Is this what you want?
<?php
/**
$12.6m ==> 12.6 million gold coins
£2b ==> 2 million platinum coins
€99 ==> 99 gold coins
*/
$str = <<<EOD
$12.6m
£2b
€99
EOD;
preg_match('/\$(.*?)m/', $str, $string1);
echo $string1[1] . " million gold coins \n";
preg_match('/\£(.*?)b/', $str, $string2);
echo $string2[1] . " million platinum coins \n";
preg_match('/\€([0-9])/', $str, $string3);
echo $string3[1] . " gold coins \n";
// output:
// 12.6 million gold coins
// 2 million platinum coins
// 9 gold coins

Matching date in string

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

Exclude a group regular expression

I search in many threads in Stackoverflow but I didn't find anything relevant for my case.
Here is the source text :
<span class="red"><span>70</span><span style="display:none">1</span><span>,89</span> € TTC<br /></span>
I want to extract 70,89 with a regular expression.
So I tried :
<span class="red"><span>([0-9]+)(<\/span><span style="display:none">1<\/span><span>)(,[0-9]+)<\/span>
which returns an array (with preg_match_all in PHP) with 3 groups :
1/ 70
2/
</span><span style="display:none">1</span><span>
3/ ,89
I would like to exclude group 2 and merge 1 & 3.
So I also tried :
<span class="red"><span>([0-9]+)(?:<\/span><span style="display:none">1<\/span><span>)(,[0-9]+)<\/span>
but it returns :
70
,89
How can I merge the two groups ?
Thanks a lot for your answers, I am going to be crazy searching for this regular expression ! :)
Have a good day !
Just match the numbers that are wrapped with a plain <span>:
$str = '<span class="red"><span>70</span><span style="display:none">1</span><span>,89</span> € TTC<br /></span>';
if (preg_match_all('#<span>([,\d]+)</span>#', $str, $matches)) {
echo join('', $matches[1]);
}
// output: 70,89

How can I match a string between two other known strings and nothing else with REGEX?

I want to extract a string between two other strings. The strings happen to be within HTML tags but I would like to avoid a conversation about whether I should be parsing HTML with regex (I know I shouldn't and have solved the problem with stristr() but would like to know how to do it with regular expressions.
A string might look like this:
...uld select “Apply” below.<br/><br/><b>Primary Location</b>: United States-Washington-Seattle<br/><b>Travel</b>: Yes, 75 % of the Time <br/><b>Job Type</b>: Standard<br/><b>Region</b>: US Service Lines: ASL - Business Intelligence<br/><b>Job</b>: Business Intelligence<br/><b>Capability Group</b>: Con/Sol - BI&C<br/><br/>LOC:USA
I am interested in <b>Primary Location</b>: United States-Washington-Seattle<br/> and want to extract 'United States-Washington-Seattle'
I tried '(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)' which worked in RegExr but not PHP:
preg_match("/(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)/", $description,$matches);
You used / as regex delimiter, so you need to escape it if you want to match it literally or use a different delimiter
preg_match("/(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)/", $description,$matches);
to
preg_match("/(?<=<b>Primary Location<\/b>:)(.*?)(?=<br\/>)/", $description,$matches);
or this
preg_match("~(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)~", $description,$matches);
Update
I just tested it on www.writecodeonline.com/php and
$description = "uld select “Apply” below.<br/><br/><b>Primary Location</b>: United States-Washington-Seattle<br/><b>Travel</b>: Yes, 75 % of the Time <br/><b>Job Type</b>: Standard<br/><b>Region</b>: US Service Lines: ASL - Business Intelligence<br/><b>Job</b>: Business Intelligence<br/><b>Capability Group</b>: Con/Sol - BI&C<br/><br/>LOC:USA";
preg_match("~(?<=<b>Primary Location</b>:)(.*?)(?=<br/>)~", $description, $matches);
print_r($matches);
is working. Output:
Array ( [0] => United States-Washington-Seattle [1] => United States-Washington-Seattle )
You can also get rid of the capturing group and do
$description = "uld select “Apply” below.<br/><br/><b>Primary Location</b>: United States-Washington-Seattle<br/><b>Travel</b>: Yes, 75 % of the Time <br/><b>Job Type</b>: Standard<br/><b>Region</b>: US Service Lines: ASL - Business Intelligence<br/><b>Job</b>: Business Intelligence<br/><b>Capability Group</b>: Con/Sol - BI&C<br/><br/>LOC:USA";
preg_match("~(?<=<b>Primary Location</b>:).*?(?=<br/>)~", $description, $matches);
print($matches[0]);
Output
United States-Washington-Seattle

PHP: How to find the beginning and end of a substring in a string?

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

Categories