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.
Related
sorry if my question was stupid, please someone help me to fix this issue.
i have string like
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
this $str_value is dynamic , it will change each page. now i need to replace 9 in this string as 10. add integer 1 and replace
for example if the $str_value = "http://99.99.99.99/var/test/src/158-of-box.html/251/"
then output should be
http://99.99.99.99/var/test/src/158-of-box.html/252/
i tried to replace using preg_match but i m getting wrong please somesone help me
$str = preg_replace('/[\/\d+\/]/', '10',$str_value );
$str = preg_replace('/[\/\d+\/]/', '[\/\d+\/]+1',$str_value );
Thank's for the answer, #Calimero! You've been faster than me, but I would like to post my answer, too ;-)
Another possibilty is to fetch the integer by using a group. So you don't need to trim $matches[0] to remove the slashes.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$str = preg_replace_callback('/\/([\d+])\//', function($matches) {
return '/'.($matches[1]+1).'/';
}, $str_value);
echo $str;
You need to use a callback to increment the value, it cannot be done directly in the regular expression itself, like so :
$lnk= "http://99.99.99.99/var/test/src/158-of-box.html/9/";
$lnk= preg_replace_callback("#/\\d+/#",function($matches){return "/".(trim($matches[0],"/")+1)."/";},$lnk); // http://99.99.99.99/var/test/src/158-of-box.html/10/
Basically, the regexp will capture a pure integer number enclosed by slashes, pass it along to the callback function which will purge the integer value, increment it, then return it for replacement with padded slashes on each side.
I'd suggest also another approach based on explode and implode instead of doing any regexp stuff. In my opinion this is more readable.
$str_value = "http://99.99.99.99/var/test/src/158-of-box.html/11/";
// explode the initial value by '/'
$explodedArray = explode('/', $str_value);
// get the position of the page number
$targetIndex = count($explodedArray) - 2;
// increment the value
$explodedArray[$targetIndex]++;
// implode back the original string
$new_str_value = implode('/', $explodedArray);
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 ));
}
I have the following in a variable, |MyString|
I want to strip the leading | and the ending | returning MyString
What is the quickest and non intensive way of doing this?
Easiest way is probably
$result = trim($input, '|');
http://docs.php.net/trim
e.g.
<?php
$in = '|MyString|';
$result = trim($in, '|');
echo $result;
prints MyString
Checkout the str_replace function in PHP http://php.net/manual/en/function.str-replace.php
this should remove all '|' characters:
str_replace('|','',$myString)
You may be able to use a regular expression to only remove the first and last '|' or alternatively using the String trim() function may also work:
http://www.php.net/manual/en/function.trim.php
So, something like this:
$trimmedMyString = trim($myString, "|");
Worth trying anyway.
assuming i have these texts 'x34' , '150px' , '650dpi' , 'e3r4t5' ... how can i get only numbers ? i mean i want 34 , 150 , 650 , 345 without any other character . i mean get the numbers this string has into one variable .
$str = "e3r4t5";
$str_numbers_only = preg_replace("/[^\d]/", "", $str);
// $number = (int) $str;
Sorry for joining the bandwagon late, rather than using Regex, I would suggest you use PHP's built in functions, which may be faster than Regex.
filter_var
flags for the filters
e.g. to get just numbers from the given string
<?php
$a = '!a-b.c3#j+dk9.0$3e8`~]\]2';
$number = str_replace(['+', '-'], '', filter_var($a, FILTER_SANITIZE_NUMBER_INT));
// Output is 390382
?>
To adhere to more strict standards for your question, I have updated my answer to give a better result.
I have added str_replace, as FILTER_SANITIZE_NUMBER_FLOAT or INT flag will not strip + and - chars from the string, because they are part of PHP's exception rule.
Though it has made the filter bit long, but it's now has less chance of failing or giving you unexpected results, and this will be faster than REGEX.
Edit:
1: Realized that with FILTER_SANITIZE_NUMBER_FLOAT, PHP won't strip these characters optionally .,eE, hence to get just pure numbers kindly use FILTER_SANITIZE_NUMBER_INT
2: If you have a PHP version less than 5.4, then kindly use array('+', '-') instead of the short array syntax ['+', '-'].
You can use a regular expression to remove any character that is not a digit:
preg_replace('/\D/', '', $str)
Here the pattern \D describes any character that is not a digit (complement to \d).
Use PHP FILTER functions if you are using PHP 5.2.X, 5.3.x,5.4 . Its highly recommended
$mixed_input = "e3r4t5";
$only_numbers = filter_var($mixed_input, FILTER_SANITIZE_NUMBER_INT);
Please Go through with this link to know more
Replace everything that isn't a number and use that value.
$str = "foo1bar2baz3";
$num = intval(preg_replace("/[^0-9]/", "", $str));
You could use the following function:
function extract_numbers($string) {
preg_match_all('/([\d]+)/', $string, $match);
return $match;
}
Is there a simple way to remove a leading zero (as in 01 becoming 1)?
You can use the ltrim function:
ltrim($str,"0");
$str = "01";
echo intval($str);
if you use the trim functions, you might mistakenly remove some other character, like by triming "12" your will have "2".
use the intval() function. this function will convert your string (which could start by a leading zero or not) to an integer value. intval("02") will be 2 and intval ("32") wll be 32.
Regex replace /^0*/ with '' for a string return solution
The exact code will be something like this
<?php
$string_number = '000304';
echo preg_replace('/^0*/', '', $string_number);
?>
Just multiply by 1
echo "01"*1