Exploding a number and separate it by a space [duplicate] - php

This question already has answers here:
Insert string at specified position
(11 answers)
separate string in two by given position
(8 answers)
Closed 4 years ago.
I want to explode a string or an integer and separate it by a space.
E.g., I have this int 12345678, and I want its numbers to become like 123 45678. I want the first three numbers separated. Can someone give me a clue or hint in how to achieve this, like what function to use in PHP? I think using an explode will not work here because the explode function needs a separator.

You can use substr_replace() - Replace text within a portion of a string.
echo substr_replace(1234567, " ", 3, 0); // 123 4567
https://3v4l.org/9CFlX

You could use substr() :
$str = "12345678" ;
echo substr($str,0,3)." ".substr($str, 3); // "123 45678"
Also works with an integer :
$int = 12345678 ;
echo substr($int,0,3)." ".substr($int, 3); // "123 45678"

This problem will solve by using substr().
The substr() function returns a part of a string.
Syntax: substr(string,start,length)
Example:
$value = "12345678";
echo substr($value,0,3)." ".substr($value, 3);
Output: 123 45678
You may get better understand from here.

Related

How to add - in-between strings [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);

PHP String Split/Explode [duplicate]

This question already has answers here:
How to limit the elements created by explode()
(4 answers)
Closed 7 months ago.
I have a String "Hello I am apple"
I want to split to two string
str1 = "Hello"
str2 = "I am apple"
so just simply split by the first space, then the following words will be in one string
Explode function has a 3rd parameter for that:
limit
If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
http://php.net/explode
list($str1, $str2) = explode(" ", $str, 2);
Same result also can be achieved by following code....
<?php
$str = "Hello I am Apple";
$str_con = explode(" ",$str,2);
echo $str_con[0];
echo $str_con[1];
?>

PHP preg_match_all to extract number with or without decimal [duplicate]

This question already has answers here:
Extract floating point numbers from a delimited string in PHP
(6 answers)
Closed 6 years ago.
I am trying to extract any number out from a string (with or without decimal) and dump that into an array. Below is my code
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/\d+/",$str,$matches);
echo var_dump($matches);
The current output has 3 elements below:
21
11
30
But I expect to output only 2 elements below:
21.11
30
How do I change my regex?
You should use this regex (add dot in character class) :
$matches = array();
$str = "I have string of 21.11 out of 30";
preg_match_all("/[\d\.]+/",$str,$matches);
var_dump($matches);
Test it (Ctrl + Enter)

How to cut string after comma [duplicate]

This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 8 months ago.
$A=123132,32132,123132321321,3
$B=1,32,99
$C=456,98,89
$D=1
I want to cut string after first comma
output. . .
$A=123132
$B=1
$C=456
$D=1
$newA = current(explode(",", $A));
from: PHP substring extraction. Get the string before the first '/' or the whole string
You can do this with strpos to get the position of the first comma, and substr to get the value prior to it:
<?php
$val='123132,32132,123132321321,3';
$a=substr($val,0,strpos($val,','));
echo $a;
?>
Output:
123132
There are a number of ways to accomplish this:
substr($A,0,strpos($A,","))
or
current(explode(",", $A))
will both return the value 123132
You can do
$A='123132,32132,123132321321,3';
$t=explode(',',$A);
$result = $t[0];

How to get rid of string elements in php? [duplicate]

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 11 months ago.
Customer id literal has customer_id+Domain_details, eg.: 998787+nl and now I just want to have 998787 and not +nl, how can this be acheived this in php
Question:
I have number like 9843324+nl and now I want to get rid of all elements including + and afterwards at the end and only have 9843324 and so how should I do this in php ?
Right now I am having $o_household->getInternalId returns me 9843324+nl but I want 9843324, how can I achieve this ?
Thanks.
Thanks.
Update :
list($customer_id) = explode('+',$o_household->getInternalId());
Will this solve my problem ?
If you don't want to keep the leading zeros, simply convert it into an integer.
$theID = (int)"9843324+nl";
// $theID should now be 9843324.
If the + is just a separator and the sutff before can be a non-number, use
$val = "9843324+nl";
$theID = substr($val, 0, strcspn($val, '+'));
// $theID should now be "9843324".
Easy way? Just cast it to an int and it will drop off the extra stuff.
<?php
$s = '998787+nl';
echo (int)$s;
?>
Output:
998787
<?php
$plusSignLoc = strpos($o_household->getInternalId, "+");
$myID = substr($o_household->getInternalId, 0, $plusSignLoc);
//Debug (Verification)
echo $myID;
?>
This will find the + sign, and insure that anything and everything after it will be removed.
If you need it to remain a string value, you can use substr to cut the string down to its starting index to the 3rd from last character, omitting the domain details +nl
$customer_id = substr($o_household->getInternalId, 0, -3);
As a slightly more general solution, this regular expression will remove everything that isn't a digit from the string $str and put the new string (set of numbers, so it can be treated as an integer) into $num
$num = preg_replace('/[^\d]/', '', $str);
Check out the explode() function, and use + as your delimiter.

Categories