PHP regular expression - number (price) only - php

I am trying to remove .00 from price field. I have price output like this:
127,000.00 €
My regex expression is:
<?php $site = get_field('price'); $site = preg_replace( '/[^0-9]/', '', $site ); $site = rtrim($site, ''); echo $site;?>
Result is:
12700000
It should be:
127000
Can anybody help me with this?

A better aproch with PHP 7+...
Ignoring the decimals:
$site = '127,000.00 €';
$site = (int) preg_replace('/\..*|\D/', NULL, $site);
echo $site;
// 127000
Rounding:
$site = '127,000.50 €';
$site = round(preg_replace('/[^\d.]/', NULL, $site));
echo $site;
// 127001

$amount = "127,000.00 €";
$amount = explode(" ",$amount);
$amount = $amount[0];
$amount = str_replace( ',', '', $amount );
echo (int) $amount ;
output: 127000

$amtstr = explode(' ', "127,000.00 €");
$value = round(str_replace(',','',$amtstr[0])).' '.$amtstr[1];
echo $value;
Hope this may help you

Related

How to filter $ value on php

i want to filter value
example:
112 to be 01:12
from script
$duration = get_post_meta( $post->ID, 'TMDB_Runtime', true );
if ( ! empty( $duration ) ) {
echo '<div class="gmr-duration-item" property="duration">' . $duration . __( ' <a class="fa fa-clock-o" aria-hidden="true"></a>','movies' ) . '</div>';
I've tried using substr and str_replace and put it into function.php
<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>
But always display parse error.
There are two problems I am noticing in this code snippet:
<?php
$text = '$duration';
$text = substr($text, 0, 3);
echo $text =str_replace("1","01:", $text);
?>
First:
echo $text =str_replace("1","01:", $text); is incorrect syntax. You can't assign a function to a function. Split it into two lines.
$text = str_replace("1","01:", $text);
echo $text;
Second:
$text = '$duration'; is not setting $text to 112. It is setting it to the literal string $duration. Single-quotes in PHP do not evaluate variables, that is what double-quotes do. So, try this instead:
$text = "$duration";
You could do it this way, statically:
<?php
$duration = "112";
$exploded = str_split($duration);
$exploded[0] = "0".$exploded[0].":";
$final = implode($exploded);
echo $final;
?>

How to concatenate string continuously in php?

<?php
$test = ' /clothing/men/tees';
$req_url = explode('/', $test);
$c = count($req_url);
$ex_url = 'http://www.test.com/';
for($i=1; $c > $i; $i++){
echo '/'.'<a href="'.$ex_url.'/'.$req_url[$i].'">
<span>'.ucfirst($req_url[$i]).'</span>
</a>';
//echo '<br/>'.$ex_url;....//last line
}
?>
OUTPUT - 1 //when comment last line
/ Clothing / Men / Tees
OUTPUT - 2 //when un-comment last line $ex_url shows
/ Clothing
http://www.test.com// Men
http://www.test.com// Tees
http://www.test.com/
1. Required output -
In span - / Clothing / Men / Tees and last element should not be clickable
and link should created in this way
http://www.test.com/clothing/Men/tees -- when click on Tees
http://www.test.com/clothing/Men -- when click on Men
...respectively
2. OUTPUT 2 why it comes like that
Try this:
<?php
$test = '/clothing/men/tees';
$url = 'http://www.test.com';
foreach(preg_split('!/!', $test, -1, PREG_SPLIT_NO_EMPTY) as $e) {
$url .= '/'.$e;
echo '/<span>'.ucfirst($e).'</span>';
}
?>
Output:
/Clothing/Men/Tees
HTML output:
/<span>Clothing</span>/<span>Men</span>/<span>Tees</span>
Try using foreach() to iterate the array and you'll have to keep track of the path after the url. Try it like so (tested and working code):
<?php
$test = '/clothing/men/tees';
$ex_url = 'http://www.test.com';
$items = explode('/', $test);
array_shift($items);
$path = '';
foreach($items as $item) {
$path .= '/' . $item;
echo '/ <span>' . ucfirst($item) . '</span>';
}
Try this.
<?php
$test = '/clothing/men/tees';
$req_url = explode('/', ltrim($test, '/'));
$ex_url = 'http://www.test.com/';
$stack = array();
$reuslt = array_map(function($part) use($ex_url, &$stack) {
$stack[] = $part;
return sprintf('%s', $ex_url, implode('/', $stack), ucfirst($part));
}, $req_url);
print_r($reuslt);
<?php
$sTest= '/clothing/men/tees';
$aUri= explode( '/', $sTest );
$sBase= 'http://www.test.com'; // No trailing slash
$sPath= $sBase; // Will grow per loop iteration
foreach( $aUri as $sDir ) {
$sPath.= '/'. $sDir;
echo ' / '. ucfirst( $sDir ). ''; // Unnecessary <span>
}
?>

php address into latitude and longitude

i am trying to convert the address into latitude.here's my code.
but its giving wrong latititude. if i put the address manually at $final place it gives the correct latitude.however i cant hard code it,whatever source comes in text box i have to find out that address latitude. what is the mistake?
<?php
$source=$_POST['textfield11'];
$dest=$_POST['textfield12'];
$time=$_POST['textfield13'];
$only = str_replace(',', '+', $source);
$final = str_replace(' ', '+', $only);
$url='http://maps.googleapis.com/maps/api/geocode/json?address=$final&sensor=false';
$source1 = file_get_contents($url);
$obj = json_decode($source1);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;
?>
Instead of using str_replace, you should use urlencode.
<?php
$source=urlencode($_POST['textfield11']);
$dest=$_POST['textfield12'];
$time=$_POST['textfield13'];
$url='http://maps.googleapis.com/maps/api/geocode/json?address=$source&sensor=false';
$source1 = file_get_contents($url);
$obj = json_decode($source1);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;
?>
<?
$final = urlencode($source);
$geo = simplexml_load_file("http://maps.google.com/maps/api/geocode/xml?address={$final}&sensor=false");
$lat = $geo->result->geometry->location->lat;
$lng = $geo->result->geometry->location->lng;
?>
Add a space after your , in the str_replace ... Cos at the moment, if the user types an address as "Somewhere road, That town" it will replace the ',' with a plus, and then the space resulting in: "Somewhere+road++That town" ... So something like this:
<?php
$source=$_POST['textfield11'];
$dest=$_POST['textfield12'];
$time=$_POST['textfield13'];
$only = str_replace(', ', '+', $source);
$final = str_replace(' ', '+', $only);
$url='http://maps.googleapis.com/maps/api/geocode/json?address=$final&sensor=false';
$source1 = file_get_contents($url);
$obj = json_decode($source1);
$LATITUDE = $obj->results[0]->geometry->location->lat;
echo $LATITUDE;
?>

php match a string from other string

I have a string as $test = 'aa,bb,cc,dd,ee' and other string as $match='cc'. I want the result as $result='aa,bb,dd,ee'.
I am not able to get te result as desired as not sure which PHP function can give the desired output.
Also if I have a string as $test = 'aa,bb,cc,dd,ee' and other string as $match='cc'. I want the result as $match=''. i.e if $match is found in $test then $match value can be skipped
Any help will be really appreciated.
You can try with:
$test = 'aa,bb,cc,dd,ee';
$match = 'cc';
$output = trim(str_replace(',,', ',', str_replace($match, '', $test), ','));
or:
$testArr = explode(',', $test);
if(($key = array_search($match, $testArr)) !== false) {
unset($testArr[$key]);
}
$output = implode(',', $testArr);
Try with preg_replace
$test = 'aa,bb,cc,dd,ee';
$match ='cc';
echo $new = preg_replace('/'.$match.',|,'.$match.'$/', '', $test);
Output
aa,bb,dd,ee
$test = 'aa,bb,cc,dd,ee';
$match='cc';
echo trim(str_replace(',,', ',' , str_replace($match,'',$test)),',');
DEMO
Try this:
$test = 'aa,bb,cc,dd,ee';
$match = 'cc';
$temp = explode(',', $test);
unset($temp[ array_search($match, $temp) ] );
$result = implode(',', $temp);
echo $result;

String translate from Array , str_replace?

String translate from Array , str_replace?
I have an array
$money = array(
"USD"=>100,
"BAT"=>1000,
"RIEL"=>2000
);
And I define as constant to be translate:
define("const_infor","Your __TYPE__ are: __AMOUNT__ __CURRENCY__ .<br><br>");
Bad WAYS:
echo "Your balance are :";//more constant here
foreach ($money as $currency=>$amount){
echo $money.$currency."; ";
}
I try to output(GOOD WAYS):
$tmp1 = "";
$tmp2 = "";
foreach ($money as $currency=>$amount){
$tmp1 .= $money;
$tmp2 .= $currency;
}
echo str_replace(ARRAY("__TYPE__","__AMOUNT__","__CURRENCY__"),ARRAY("Balance",$tmp1,$tmp2),const_infor);
BUT What I want is the output should be :
Your Balance are: 100 USD; 1000 BAT; 2000 RIEL
How can I pass the $currency. to str_replace ?
Anyone can help me to do this.?
I don't know what exactly you wanna do but if it's only output
try
printf("Your Money %f %f %f", $money["USD"], $money["BAT"], $money["RIEL"]);
Well, below is just kind of a parser for doing what you want.. Try and see if it fits your needs:
function replace($string, $name = '', $value = '')
{
if ( !empty($name) )
{
str_replace('{'.$name.'}', $value, $string);
}
}
$string = 'Your balance is {bal1} USD, {bal2} BAT';
$string = replace('bal1', $money['USD'], $string);
$string = replace('bal2', $money['BAT'], $string);
$string = replace('bal3', $money['GBP'], $string);
print $string;
try that:
foreach ($money as $key => $cur)
echo $cur.' '. $key;

Categories