How to cut string after comma [duplicate] - php

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];

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);

How can we convert string into array in php? [duplicate]

This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 1 year ago.
I have a string as mention below
$string = 20181123091338,20181130070940;
Now, I want to convert this string into an array format like
$array = array("20181123091338","20181130070940");
So, How can I do this? Please help me.
Thank You
You can use explode() .
Try this
$string = "20181123091338,20181130070940";
$arr = explode(",", $string);
echo "<pre>"; print_r($arr);
Explaination
Here we are exploding string by ","(comma), so we are passing ,(comma) as first parameter in explode function and string passing as second parameter.

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

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.

PHP ltrim() not working as expected [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);

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