How to make str_replace work in printf? - php

I am trying to replace the underscore character in printf. But it might be missing something because it does not work. Does anyone know how to fix this?
printf("%s",str_replace($fieldinfo,"_"," ")->name);

Hope this will work.
Wrong:
str_replace($fieldinfo,"_"," ")->name
Here you can not treat a string or array as an object.
Note: str_replace returns either string or array but not object;
printf("%s",str_replace("-","_","some-value")); //some_value
echo printf("%s",str_replace("-","_","some-value")); //some_value10 //10 is length of string

Hope this will help,
considering $fieldinfo has this value "sample_value_to_output"
$fieldinfo = "sample_value_to_output";
printf("%s",str_replace("_"," ",$fieldinfo));
this will output
sample value to output

your str_replace code is wrong
str_replace($fieldinfo,"_"," ")
it should be
str_replace("_"," ",$fieldinfo->name)

Related

How to convert 水瀬いのり to %e6%b0%b4%e7%80%ac%e3%81%84%e3%81%ae%e3%82%8a in php?

i wonder, how to convert character 水瀬いのり to %e6%b0%b4%e7%80%ac%e3%81%84%e3%81%ae%e3%82%8a in php?
also 보이프렌드 to %eb%b3%b4%ec%9d%b4%ed%94%84%eb%a0%8c%eb%93%9c ?
thanks
This one would help urlencode
See here for an example:
$str = '水瀬いのり';
var_dump( urlencode($str) );
If you must use the urlencode() function for that.
echo urlencode('보이프렌드');
// output: %EB%B3%B4%EC%9D%B4%ED%94%84%EB%A0%8C%EB%93%9C
If you really need that the result will be in lowercase then you also must to use the strtolower() function.
echo strtolower(urlencode('보이프렌드'));
// output: %eb%b3%b4%ec%9d%b4%ed%94%84%eb%a0%8c%eb%93%9c

PHP converting variable string to int

To start, I could not find this answer online because of the way my variable string is defined. Normally I should be able to add 0 to the variable, or use (int), but it does not work.
<?php
$casestringid = "'118'";
$caseid = $casestringid + 0;
echo $casestringid;
echo $caseid;
?>
Output: '118'0
As you can see, because of the way my first variable is declared, the standard methods of converting a string to an integer does not work. My $casestringid is written like that because it requests a number from another page. Rather than trying to change how to format that, I figure it will be easier for help on how to convert a string that looks like that, into an integer. I would like the output of caseid to be 118. Thanks for any help in advance.
The problem is that '118' is not an integer as far as the PHP parser is concerned, it's a string. It looks like an integer to us, of course, but it has slashes (') which make it "unconvertible".
Use str_replace for this:
intval(str_replace("'", '', $casestringid));
i think you have no other chance like this:
intval(str_replace("'",'',$casestringid));
Replace the '':
intval(str_replace("'",'',$casestringid));
Try intval ($casestringid) + 0.
EDIT:
How about this, then:
filter_var ($casestringid, FILTER_SANITIZE_NUMBER_INT);
You have to remove the single quotes and use intval().
<?php
$casestringid = "'118'";
$parseid = str_replace("'", "", $casestringid);
$caseid = intval($parseid);
echo $casestringid;
echo $caseid;
?>
$casestringid = "'118'";
$int = str_replace("'", "", $casestringid);
echo intval($int);
If it is just an integer you are looking for, this could work.
it will remove any non digit characters then return it as an int
function parseInt( $s ){
return (int)(preg_replace( '~\D+~' , '' , $s ));
}

preg_match returning complete string

I am trying to get numbers from a string but i keep getting the complete string with numbers and not numbers alone
$string = "stuff here with id=485&other=123";
preg_match('(id=\d+)',$string,$match);
the above results like id=485 but i just want 485
Any help would be appreciated guys.
The parentheses say what to collect. You also need to use a delimiter at either end
preg_match('/id\=(\d+)/',$string,$match);
print_r($match);
/* should be
0=>"id=485",
1=>"485"
*/
echo $match[1];
Edit: See cryptic's answer, parse_str is most likely faster than preg_match. Most things are.
$string = "stuff here with id=485&other=123";
parse_str(strstr($string, 'id='), $output);
echo $output['id']; // 485
Without using a regex.

How to get rid of extra elements in string literal in php?

I have number like 9843324+ and now I want to get rid of + at the end and only have 9843324 and so how should I do this in php ?
Right now I am doing $customer_id = explode('+',$o_household->getInternalId); also $o_household->getInternalId returns me 9843324+ but I want 9843324, how can I achieve this ?
Thanks.
if you just want to pop the + off the end, use substr.
$customer_id = substr($o_household->getInternalId, 0, -1);
or rtrim
$customer_id = rtrim($o_household->getInternalId, "+");
You can use a regeular expression to remove anything that isn't a number
$newstr = preg_replace("/[^0-9]+/","",$str);
$customer_id = rtrim ( $o_household->getInternalId, '+' );
rtrim function reference
rtrim removes the characters in the second argument (+ only in this case) from the end of a string.
In case there is no + at the end of the string, this won't mess up your value like substr.
This solution is obviously more readable and faster than, let's say preg_replace too.
you could use intval($o_household->getInternalId)
Is there a reason you need to use explode? i would just use substr as Andy suggest or str_replace. Either would work for the example you provided.
You can use intval to get the integer value of a variable, if possible:
echo intval($str);
Be aware that intval will return 0 on failure.

How to remove 0's from a string

I'm looking at the function trim but that unfortunately does not remove "0"s how do I add that to it? Should I use str_replace?
EDIT:
The string I wanted to modify was a message number which looks like this: 00023460
The function ltrim("00023460", "0") does just what I need :) obviously I would not want to use the regular trimbecause it would also remove the ending 0 but since I forgot to add that the answer I got is great :)
$ php -r 'echo trim("0string0", "0") . "\n";'
string
For the zero padded number in the example:
$ php -r 'echo ltrim("00023460", "0") . "\n";'
23460
The trim function only removes characters from the beginning and end of a string, so you probably need to use str_replace.
If you only need to remove 0s from the beginning and end of the string, you can use the following:
$str = '0000hello00';
$clean_string = trim($str, '0'); // 'hello'
This should have been here from the start.
EDIT: The string I wanted to modify
was a message number which looks like
this: 00023460
The best solution is probably this for any integer lower then PHP_INT_MAX
$number = (int) '00023460';
Take a look at str_replace
print str_replace( '0', '', "This0string0needs0spaces");
Sure it does (see second parameter $charlist):
trim('000foo0bar000', '0') // 'foo0bar'

Categories