How can I do the following with php?
This is my example:
http://www.example.com/index.php?&xx=okok&yy=no&bb=525252
I want remove this part: &yy=no&bb=525252
I just want this result:
http://www.example.com/index.php?&xx=okok
I tried this :
$str = 'bla_string_bla_bla_bla';
echo preg_replace('/bla_/', '', $str, 1); ;
but this not what I want.
Going for preg_replace was a good start. But you need to learn about regexes.
This will work:
$str = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
echo preg_replace ('/&yy.+$/', '', $str);
Here the regex is &yy.+$
Let's see how this works:
&yy matches &yy obviously
.+ matches everything ...
$ ... until the end of the string.
So here, my replacement says : Replace whatever begins by &yy until the end of the string by nothing, which is actually simply deleting this part.
You can do this:
$a = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
$b = substr($a,0,strpos($a,'&yy')); // Set in '&yy' the string to identify the beginning of the string to remove
echo $b; // Will print http://www.example.com/index.php?&xx=okok
Are you always expecting the end part to have the 'yy' variable name? You could try this:
$str = 'http://www.example.com/index.php?&xx=okok&yy=no&bb=525252';
$ex = explode('&yy=', $str, 2);
$firstPart = $ex[0];
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
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
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.
I have dynamic link build from variable :
/Tinkle/Matte/BlackHyper/Black/Gunmetal
How can I remove all text after variable value "BlackHyper" to become:
"/Tinkle/Matte/BlackHyper"
I try rtrim :
$param="BlackHyper";
$str="/Tinkle/Matte/BlackHyper/Black/Gunmetal";
rtrim($str,$param);
No luck , remove some letters....
RTrim doesnt work like this.
You need to do something like this
$pos = strpos($str, $param);
$endpoint = $pos + strlen($param);
$newStr = substr($str,0,$endpoint );
This will create a new string (there might be a bug or two I havent tested it) with all characters up to your param.
Maybe you can use strstr function?
$a = "before/after";
$b = strstr($a, "/",true); // gets text before /
$c = strstr($a, "/"); // gets text after /