php preg_replace using $ from the end - php

I am trying to hip of from the end dot or comma and zero's using php
String is €69,00
Code is return preg_replace('/[\.,]{1}0*$/', '', $_price);
Search from right using dollar; find multipl zero's until you find the first comma or dot (from the right). Replace it with empty string.
Original request: show a formatted price that is integer, as a floor value without decimals.
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
Somehow this is not working
question: How can I fix the regex to make integer whole values show without the decimals when the string is already formatted (not a number)

Another variation would be to look only for ,00:
(€\d+)(?:[,.]00)
... and replace this with $1, see a demo on regex101.com.
Which in PHP would be:
<?php
$string = <<<DATA
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
DATA;
$regex = '~(€\d+)(?:[,.]00)~';
$string = preg_replace($regex, "$1", $string);
echo $string;
?>

try this one
preg_replace('/([€$])(\d+)([.,])(\d+)/', '$1$2', $_price);

Related

Change 10.28 by 1028

I have a problem with a string to convert in number. I am not good with this elements !\d+!
I used that but the apporach is not correct.
Thank you.
preg_match_all('!\d+!', $product_price[$i], $matches);
$price_extracted = (float)implode('.', $matches[0]);
$item['normal_price'] = $price_extracted;
if ($item['normal_price'] > 800) ......
I have this result
1 299,99 $ (orginal) is converted in 1.2999 and must be 1299.99
549,99 $ (orginal) is converted in 549.99 and must be 549.99
44,99 $ (orginal) is converted in 44.99 and must be 44.99
The problem with your approach is, that you put the digits that are not separated by anything into an array.
This means that with the first string that you provided, where the thousand dollars is seperated by a whitespace is being registered as one of these matches.
preg_match_all('!\d+!', '1 299,99 $', $matches) -> returns an array as follows:
$matches[0] = 1
$matches[1] = 299
$matches[2] = 99
If you take my approach though and first replace all whitespaces by nothing and then split the numbers into the array...:
preg_match_all('!\d+!', preg_replace('/\s/', '', '1 299,99 $'), $matches) -> returns following array:
$matches[0] = 1299
$matches[1] = 99
after that you can still implode them:
$price_exctracted = (float)implode(".", $matches);
EDIT
A little explanation about preg_replace, preg_match_all and regex:
The regex '!\d+!' (I don't actually know why there would be '!' instead of '/' but if it works...) searches for digits (\d). The "+" refers to "one or more". So the line
preg_match_all('!\d+!', 'someString', $myArray)
could be translated into english as follows:
Find all occurances of digits, be it one or more,
and put these occurances separated into one index of $myArray.
The second regex used in my solution, '/\s/' , is used to search for whitespaces. The "preg_replace"-function is an easy "find and replace" function concluding in:
preg_replace('/\s/', '', 'someString')
translated to english:
Find all occurances of whitespaces and replace them with nothing in 'someString'
For reference:
preg_match_all
preg_replace
regex cheat sheet
Conditions can be checked on:
PHP Live Regex

Replace a string with a result from MySQL

How would I replace a string like: {snippet:1} with a MySQL result that uses the number (in this case 1) to select the row from a table that has 1 as its ID?
Thanks
I would use a regex for to separate out the integer from your string.
$string = '{snippet:1}';
preg_match('/\{snippet:(\d+)\}/', $string, $match);
echo $match[1];
Output:
1
The /s are delimiters telling where the regex starts and ends. The \d is any number and the + is a quantifier telling it that there can be one or more numbers. The \s are making the {}s literal.
Regex101 Demo: https://regex101.com/r/cB9oN1/1
If this were JSON though...
$string = '{"snippet":"1"}';
$json_value = json_decode($string);
echo $json_value->snippet;
Output:
1

How to strip and get the code between two lines in PHP?

I'm trying to assign it to a variable in PHP, but all the Regex and preg_replace I've tried doesn't help me. Here is a sample text.
Claim Code:
7241B-2HWRXR9-2P2BA
$1.00
I want to pull out exactly what is in the middle, which is 7241B-2HWRXR9-2P2BA.
You can use the following to match:
Claim Code:\s*([\w-]+)\s*\$(\d+(?:\.\d+)?)
And you can pull out whatt you want by $1
See DEMO
If the code has always the same format (5-7-5 chars), you can use:
$str = 'Claim Code:
7241B-2HWRXR9-2P2BA
$1.00';
preg_match('~[\w]{5}\-[\w]{7}\-[\w]{5}~', $str, $matches);
echo $matches[0]; // returns 7241B-2HWRXR9-2P2BA
UPDATE
For optional code length this regex is possible:
preg_match('~.*:\s+([\w\-]{10,25})\s+.*~', $str, $matches);
echo $matches[1]; // ^^ here set the min and max code length, or remove it
// without setting min/max code length:
preg_match('~.*:\s+([\w\-]+)\s+.*~', $str, $matches);
Less elegant than a regexp / preg_replace, but this should work, too: Put the string into an array, then only use line 2 (array element number 1).
<?php
$string = 'Claim Code: ...............';
$lines = explode("\n", $string); //Transform the string into an array, separated by new lines (\n) (each index in the array is a single line from the string
echo $lines[1]; //this is 2nd line of the string, i.e. the claim code

Remove first instance of substring if its infront of string

I have 2 types of strings:
String 1
<br/>Ask Me A Question<br />
|<br/>Search My Apartments<br/>
String 2
Ask Me A Question<br />
|<br/>Search My Apartments<br/>
How do I have a function that remove the first <br/> from the String 1 to get String 2, while not touching anything in String 2 if String 2 is passed into the function?
start your regex with ^ to match the beginning of the string.
preg_replace('/^<br\s?\/>/', '', $string)
EDIT: whoops, had an extra space (\s) in there!
EDIT 2: added an optional space back in!
If you want to avoid using a regular expression you can check if <br/> occurs at the start of the string with strpos(). If so, simply lop off the first five characters using substr()
if (strpos($string, '<br/>') === 0) {
$string = substr($string, 5);
}
Replace `^<br/> with the empty string.
#mathletics Answer is not correct for the example given in the question, This one works:
preg_replace('#^(<br/>)#iu', '', $string);

Get integer value from malformed query string

I'm looking for an way to parse a substring using PHP, and have come across preg_match however I can't seem to work out the rule that I need.
I am parsing a web page and need to grab a numeric value from the string, the string is like this
producturl.php?id=736375493?=tm
I need to be able to obtain this part of the string:
736375493
$matches = array();
preg_match('/id=([0-9]+)\?/', $url, $matches);
This is safe for if the format changes. slandau's answer won't work if you ever have any other numbers in the URL.
php.net/preg-match
<?php
$string = "producturl.php?id=736375493?=tm";
preg_match('~id=(\d+)~', $string, $m );
var_dump($m[1]); // $m[1] is your string
?>
$string = "producturl.php?id=736375493?=tm";
$number = preg_replace("/[^0-9]/", '', $string);
Unfortunately, you have a malformed url query string, so a regex technique is most appropriate. See what I mean.
There is no need for capture groups. Just match id= then forget those characters with \K, then isolate the following one or more digital characters.
Code (Demo)
$str = 'producturl.php?id=736375493?=tm';
echo preg_match('~id=\K\d+~', $str, $out) ? $out[0] : 'no match';
Output:
736375493
For completeness, there 8s another way to scan the formatted string and explicitly return an int-typed value. (Demo)
var_dump(
sscanf($str, '%*[^?]?id=%d')[0]
);
The %*[^?] means: greedily match one or more non-question mark characters, but do not capture the substring. The remainder of the format parameter matches the literal sequence ?id=, then greedily captures one or more numbers. The returned value will be cast as an integer because of the %d placeholder.

Categories