I have this string: $entitlemnet = '43857403,erot,43857403,erot,rejh'
I want to remove all the duplicate values from this string. How can i do that?
See code below.
$roql_Ent_result = RNCPHP\ROQL::query($EntQuery)->next();
while ($Ent_result = $roql_Ent_result->next())
{
$Entitlement = $Ent_result['Entitlement'];
$findme = $Entitlement.",";
$pos = stripos($EntitlementString, $findme);
if ($pos === false)
{
$EntitlementString = $EntitlementString.$Entitlement.", ";
}
}
if ($EntitlementString != "")
{
$EntitlementString = substr($EntitlementString,0,(strlen($EntitlementString)-2)).";";
}
If you simply want to remove the duplicates from the string.
$entitlemnet = '43857403,erot,43857403,erot,rejh';
$unique_string = implode(',', array_unique(explode(',', $entitlemnet)));
Turn your string into an array by splitting it with explode() by
delimiter ','
Remove duplicate values with the function array_unique()
Turn the array back into a string by concatenating the array values with
delimiter ','
Try this
$entitlemnet = "43857403,erot,43857403,erot,rejh";
$result = implode(',', array_unique(explode(',', $entitlemnet)));
echo $result;
I think you can do it like this:
$entitlemnet = "43857403,erot,43857403,erot,rejh";
$result = implode(',', array_unique(explode(',', $entitlemnet)));
echo $result;
Will result in:
43857403,erot,rejh
Related
I am running this code:
$stmt = $pdo_conn->prepare("SELECT * from admin where support_emails = :support_emails and logged = :logged and disabled = :disabled ");
$stmt->execute(array(':support_emails' => 'Y', ':logged' => 'in', ':disabled' => ''));
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($records) > 0) {
foreach($records as $records2) {
if(filter_var($records2["email"], FILTER_VALIDATE_EMAIL)) {
$SupportEmailList = $records2["email"].', '.$SupportEmailList;
}
if(!empty($SupportEmailList)) {
$SupportEmailList = substr($SupportEmailList, 0, -2); // removes last 2 characters (`, `) from end of string
}
}
}
which returns
email1#domain1.com, email2#domain2.com
when i run in SQL
i have added:
if(!empty($SupportEmailList)) {
$SupportEmailList = substr($SupportEmailList, 0, -2); // removes last 2 characters (`, `) from end of string
}
to remove the ', ' from the end of the string if the last result doesn't exist, but it seems to be creating the following:
email1#domain1.com, email2#domain2.c
rather than:
email1#domain1.com, email2#domain2.com
I would suggest keeping your results in an array and use implode (or join) to join them with a comma.
This way you don't have to deal with trailing comma issue.
$email_address = array();
foreach($records as $records2) {
if(filter_var($records2["email"], FILTER_VALIDATE_EMAIL)) {
array_push($email_address, $records2["email"]);
}
}
$comma_separated_email_address = implode(",", $email_address);
echo $comma_separated_email_address ;
Try trim(). You can pass a list of characters as a second parameter to choose what you want to trim from the beginning/end of the string:
$example_1 = 'email1#domain1.com, email2#domain2.com';
$example_2 = 'email1#domain1.com, email2#domain2.com, ';
$example_1 = trim($example_1, ', ');
$example_2 = trim($example_2, ', ');
var_dump($example_1); // string(38) "email1#domain1.com, email2#domain2.com"
var_dump($example_2); // string(38) "email1#domain1.com, email2#domain2.com"
var_dump(explode(', ', $example_1));
// array(2) { [0]=> string(18) "email1#domain1.com" [1]=> string(18) "email2#domain2.com" }
You could split your string with explode and implode again
$data = explode(',', $elements);
$cleanedData = implode(',', $data);
You could trim space in your data trimming all values in $data array
for ($i = 0; $i < count($data); $i++)
$data[$i] = trim($data[$i]);
I don't understand your question, remove ',' if it doesn't exist? anyway You should fetch your data using PDO::FETCH_NUM. This way you'll get an array of emails like ['email1#domail.com', 'email2#mail.com'] and then if you whant something like 'email1#domail.com, email2#mail.com' use implode to join the elements with a String.
Do it:
$emails = " email1#domain1.com , fsdfsdffdsffds , email2#domain2.com,sddsfsdsd ";
$emails = array_map('trim',explode(',',$emails));
$out = array_map(function($email){
return (filter_var($email, FILTER_VALIDATE_EMAIL)) ? $email : null;
},$emails);
$emails = implode(',',array_filter($out));
print_r($emails);
// email1#domain1.com,email2#domain2.com
I am doing an assignment on how to take a string of text separated by commas and reverse the individual words and return the words in the same order.
This code does that but it is not returning it as a string for some reason and i do not understand.
<?php
function bassAckwards($input)
{
// YOUR CODE HERE
$commas = substr_count($input, ",");
$NumWords = ($commas + 1);
$words = array($input);
for($x=0;$x<$NumWords;$x++)
{
$answer = array(strrev($words[$x]));
$answer = implode(",",$answer);
print $answer;
}
}
?>
function bassAckwards($str){
$words = explode(',', $str);
$reversedWords = array_map('strrev', $words);
return implode(',', $reversedWords);
}
var_dump(bassAckwards('foo,bar,baz')); // string(11) "oof,rab,zab"
Save yourself some headaches and use the built-it functions.
explode
make 'foo,bar,baz' => array('foo','bar','baz')
array_map & strrev
Execute strrev (string reverse) on every element of the array with array_map and return the [modified] array back.
implode
convert the array back to a csv.
$reversedWords = array();
// Explode by commas
$words = explode(',', $input);
foreach ($word in $words) {
// For each word
// Stack it, reversed, in the new array $reversedWords
$reversedWords[] = strrev($word);
}
// Implode by commas
$output = implode(',', $reversedWords);
print $output;
i'm still quite new to programming, so please excuse me.
I need to do the following:
Right now i have a string being output with two value: one with characters and numbers, a comma and the second with just a boolean value.
9fjrie93, 1
I would like to trim the string so it just ourputs as the boolean value. e.g.
1
I then need to perform an equality check on the boolean value. If it's 1 do one thing, else do something else. Would a simple if statement suffuce?
Thanks very much
No need for explode if it's always the last character.
<?php
$val = '9fjrie93, 1';
if( substr( $val, -1 ) === '1' ) {
// do stuff.
}
else {
// do stuff. Just other stuff.
}
How about:
$vals = explode(", ", "9fjrie93, 1");
if ($vals[1]) ...
You can also use list to name the results returned:
$original_str = '9fjrie93, 1';
list($characters, $boolean) = explode(', ', $original_str);
echo $boolean;
Try this:
$str = '9fjrie93, 1';
$str = explode(', ', $str); //it returns array('9fjrie93', '1')
$str = end($str); //takes the last element of the array
$mystring = '9fjrie93, 1';
$findme = ',';
$pos = strpos($mystring, $findme);
$i= substr($mystring,$pos);
$string = "9fjrie93, 1";
$val = substr($string, (strrpos($string, ',')+1));
Don't use explode since it's slower
If the 1 is always at the end of the line, you can do:
$line = '9fjrie93, 1'
if ( $line[strlen([$line])-1] == '1' ) {
// do stuff
} else {
// do other stuff
}
Which should at least perform better than explode, str_replace and substr solutions.
$pos = strpos($source_str, ',');
substr($source_str, $x_pos + 1);
hope this will solve it.
$output = str_replace(',', '', strstr('9fjrie93, 1', ','));
if( $output == '1' ) {
....do something
} else {
...do something else
}
My string is like the following format:
$string =
"name=xxx&id=11&name=yyy&id=12&name=zzz&id=13&name=aaa&id=10";
I want to split the string like the following:
$str[0] = "name=xxx&id=11";
$str[1] = "name=yyy&id=12";
$str[2] = "name=zzz&id=13";
$str[3] = "name=aaa&id=10";
how can I do this in PHP ?
Try this:
$matches = array();
preg_match_all("/(name=[a-zA-Z0-9%_-]+&id=[0-9]+)/",$string,$matches);
$matches is now an array with the strings you wanted.
Update
function get_keys_and_values($string /* i.e. name=yyy&id=10 */) {
$return = array();
$key_values = split("&",$string);
foreach ($key_values as $key_value) {
$kv_split = split("=",$key_value);
$return[$kv_split[0]] = urldecode($kv_split[1]);
}
return $return;
}
$string = "name=xxx&id=11&name=yyy&id=12&name=zzz&id=13&name=aaa&id=10";
$arr = split("name=", $string);
$strings = aray();
for($i = 1; $i < count($arr), $i++){
$strings[$i-1] = "name=".substr($arr[$i],0,-1);
}
The results will be in $strings
I will suggest using much simpler term
Here is an example
$string = "name=xxx&id=11;name=yyy&id=12;name=zzz&id=13;name=aaa&id=10";
$arr = explode(";",$string); //here is your array
If you want to do what you asked, nothing more or less , that's explode('&', $string).
If you have botched up your example and you have a HTTP query string then you want to look at parse_str().
What's the best way to accomplish the following.
I have strings in this format:
$s1 = "name1|type1"; //(pipe is the separator)
$s2 = "name2|type2";
$s3 = "name3"; //(in some of them type can be missing)
Let's assume nameN / typeN are strings and they can not contain a pipe.
Since I need to exctract the name / type separetly, I do:
$temp = explode('|', $s1);
$name = $temp[0];
$type = ( isset($temp[1]) ? $temp[1] : '' );
Is there an easier (smarter whatever faster) way to do this without having to do isset($temp[1]) or count($temp).
Thanks!
list($name, $type) = explode('|', s1.'|');
Note the order of arguments for explode()
list($name,$type) = explode( '|',$s1);
$type will be NULL for $s3, though it will give a Notice
I'm a fan of array_pop() and array_shift(), which don't error out if the array they use is empty.
In your case, that would be:
$temp = explode('|', $s1);
$name = array_shift($temp);
// array_shift() will return null if the array is empty,
// so if you really want an empty string, you can string
// cast this call, as I have done:
$type = (string) array_shift($temp);
There is not need to do isset since $temp[1] will exist and content an empty value. This works fine for me:
$str = 'name|type';
// if theres nothing in 'type', then $type will be empty
list($name, $type) = explode('|', $str, 2);
echo "$name, $type";
if(strstr($temp,"|"))
{
$temp = explode($s1, '|');
$name = $temp[0];
$type = $temp[1];
}
else
{
$name = $temp[0];
//no type
}
Maybe?