how do i remove the matched character from a string? - php

here is my code
$a = "Hey there, how do i remove all, the comma from this string,";
$a = str_replace($a,',','';)
echo $a;
i want to remove all the commas available in the string, how do i do it?

$a = str_replace(",", "", $a);

$a = "Hey there, how do i remove all, the comma from this string,";
$a = str_replace(',','',$a);
echo $a;
Misplaced semi-colon and wrong function arguments.

Related

PHP str_replace not working with query

I'm having this:
$a = "t4.length = "50" AND t4.type = "F" AND (t3.minutes*60*1000+t3.seconds*1000+t3.milliseconds) < 22000";
I want to replace this string with other string, I tried str_replace but this function doesn't replace this string.
I'm trying this.
$c = str_replace($a , '', $b);
Wrong code
$c = str_replace($b , '', $a);
$b is key to find and replace
'' is replacement
$a is subject to replace
http://php.net/manual/en/function.str-replace.php
It was my fault, there were other extra spaces that's why this function was not working. I truncated all extra spaces then it was working fine.

Remove identical succesive characters from string PHP

I have this string
$string = "000000014Y00j:7";
I want to turn it into
$string = "14Y00j:7";
I want to remove all the zeros from the start of the string, in PHP
If you want to delete any chars (not only 0) you could use
$a = "000000014Y00j:7";
if($a[0]==$a[1]){
$a = ltrim($a,$a[0]);
}
echo $a;
$str = "000000014Y00j:7";
$str = ltrim($str, '0');
echo $str ;

PHP delete everything after comma (including comma)

I just can't get my head around the syntax for this. I have an array $foo and I call it within a function: wp_list_pages($foo); (this is a wordpress site).
I found this code online but can't get it to work...
I've tried both of these ways and it doesn't seem to do anything. Can anyone help?
<?php $mystring = $foo;
list ($before,$after) = split (',', $mystring);
echo $before;
?>
<?php wp_list_pages( $foo ); ?>
or this:
<?php $has_comma = (stristr($foo, ",")>-1) ? 1 : 0;
if ($has_comma) {
list ($before,$after) = split (',', $foo);
$foo = $before;
} ?>
<?php wp_list_pages( $foo ); ?>
if (false !== ($pos = strpos($foo, ','))) {
$foo = substr($foo, 0, $pos);
}
or if you're sure that there is always coma, it can be simplier:
$foo = substr($foo, 0, strpos($foo, ','));
assuming $foo has some string in it for example "This is a short, boring sentence", then:
list($before,$after) = split(',',$mystring)
//$before => "This is a short"
//$after => "boring sentence"
echo $before; //prints "This is a short"
If you are not seeing this behavior you need to make sure $foo has some string in it.
That example is a bit too convoluted. Here's a simpler one:
<?php
$mystring = "a,b,c,d egg";
$n = strpos($mystring, ',');
echo substr($mystring, 0, $n)
?>
This one just finds the index of the first "," in $mystring and gets the string up until that point.
If you want an array-based example, here is one that uses explode, since split is deprecated:
<?php
$mystring = "a,b,c,d egg";
$array = explode(',', $mystring);
echo $array[0];
?>
So explode splits $mystring on "," so that each item separated by a "," becomes its own element. Naturally the first item is the 0th index, so that's how you truncate after the commas.
split() is deprecated, try use preg_split(), or str_split(), see in php manual: http://www.php.net/manual/ro/function.split.php

PHP remove commas from numeric strings

In PHP, I have an array of variables that are ALL strings. Some of the values stored are numeric strings with commas.
What I need:
A way to trim the commas from strings, and ONLY do this for numeric strings. This isn't as straightforward as it looks. The main reason is that the following fails:
$a = "1,435";
if(is_numeric($a))
$a = str_replace(',', '', $a);
This fails because $a = "1435" is numeric. But $a = "1,435" is not numeric. Because some of the strings I get will be regular sentences with commas, I can't run a string replace on every string.
Do it the other way around:
$a = "1,435";
$b = str_replace( ',', '', $a );
if( is_numeric( $b ) ) {
$a = $b;
}
The easiest would be:
$var = intval(preg_replace('/[^\d.]/', '', $var));
or if you need float:
$var = floatval(preg_replace('/[^\d.]/', '', $var));
Not tested, but probably something like if(preg_match("/^[0-9,]+$/", $a)) $a = str_replace(...)
It sounds like the ideal solution for what you're looking for is filter_var():
$a = filter_var($a, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
(Note that it's using FILTER_VALIDATE_FLOAT instead of FILTER_VALIDATE_INT because that one doesn't currently have a FILTER_FLAG_ALLOW_THOUSAND option).
Try this .this worked for me
number_format(1235.369,2,'.','')
if you use number_format like this
number_format(1235.369,2) answer will be 1,235.37
but if you use like below
number_format(1235.369,2,'.','') answer will be 1235.37
it's removing the "," of "1,235.37"
function cleanData($a) {
if(is_numeric($a)) {
$a = preg_replace('/[^0-9,]/s', '', $a);
}
return $a;
}
If you want to remove commas from numbers inside a string that also contains words, the easiest way I think would be to use preg_replace_callback:
Example:
$str = "Hey hello, I've got 12,500 kudos for you, spend it well"
function cleannr($matches)
{
return str_replace("," , "" , $matches["nrs"]);
}
$str = preg_replace_callback ("/(?P<nrs>[0-9]+,[0-9]+)/" , "cleannr" , $str);
Output:
"Hey hello, I've got 12500 kudos for you, spend it well"
In this case the pattern (regex) differs from the one given in the accepted answer since we don't want to remove the other commas (punctuation).
If we'd use /[0-9,]+/ here instead of /[0-9]+,[0-9]+/ the output would be:
"Hey hello I've got 12500 kudos for you spend it well"
How about this:
/**
* This will parse the money string
*
* For example 1, 234, 456.00 will be converted to 123456.00
*
* #return
*/
function parseMoney(string $money) : float
{
$money = preg_replace('/[ ,]+/', '', $money);
return number_format((float) $money, 2, '.', '');
}
Example;
parseMoney('-1, 100, 000.01'); //-1100000.01

PHP Hash Removal

How can I remove all hashes from a string with PHP?
I've tried str_replace("#","",$message), but it didn't work.
Remember that str_replace() return replaced text.
Example:
$a = '### Foo ###';
$a = str_replace('#', '', $a);
echo $a;
DEMO

Categories