I have a variable that contains comma separated strings and I would like to create a check if this variable has duplicate strings inside without converting it into an array. If it would make it any easier, each comma separated strings have 3 characters.
example.
$str = 'PTR, PTR, SDP, LTP';
logic: if any of the strings has a duplicate value then display an error.
This should work for you:
Just use strtok() to loop through each token of your string, with , as delimiter. Then use preg_match_all() to check if the token is more than once in the string.
<?php
$str = "PTR, PTR, SDP, LTP";
$tok = strtok($str, ", ");
$subStrStart = 0;
while ($tok !== false) {
preg_match_all("/\b" . preg_quote($tok, "/") . "\b/", substr($str, $subStrStart), $m);
if(count($m[0]) >= 2)
echo $tok . " found more than 1 times, exaclty: " . count($m[0]) . "<br>";
$subStrStart += strlen($tok);
$tok = strtok(", ");
}
?>
output:
PTR found more than 1 times, exaclty: 2
You are going to run into some issues with just using explode. In your example, if you use explode, you'll get:
'PTR', ' PTR', ' SDP', ' LTP'
You have to map trim in there.
<?php
// explode on , and remove spaces
$myArray = array_map('trim', explode(',', $str));
// get a count of all the values into a new array
$stringCount = array_count_values($myArray);
// sum of all the $stringCount values should equal size of $stringCount IE: they are all 1
$hasDupes = array_sum($stringCount) != count($stringCount);
?>
Related
I have searched everywhere but can't find a solution that works for me.
I have the following:
$bedroom_array = array($studio, $one_bed, $two_bed, $three_bed, $four_bed);
For this example lets say:
$studio = '1';
$one_bed = '3';
$two_bed = '3';
I then use the implode function to put a comma in between all the values:
$bedroom_list = implode(", ", array_filter($bedroom_array));
echo $bedroom_list;
This then outputs:
1, 2, 3
What I want to do is find the last comma in the string and replace it with an &, so it would read:
1, 2 & 3
The string will not always be this long, it can be shorter or longer, e.g. 1, 2, 3, 4 and so on. I have looked into using substr but am not sure if this will work for what I need?
Pop off the last element, implode the rest together then stick the last one back on.
$bedroom_array = array('studio', 'one_bed', 'two_bed', 'three_bed', 'four_bed');
$last = array_pop($bedroom_array);
$string = count($bedroom_array) ? implode(", ", $bedroom_array) . " & " . $last : $last;
Convert & to the entity & if necessary.
if you have comma separated list of words you may use:
$keyword = "hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf, sdgdfg";
$keyword = preg_replace('/,([^,]*)$/', ' & \1', $keyword);
echo $keyword;
it will output:
hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf & sdgdfg
A one-liner alternative, that will work for any size array ($b = $bedroom_array):
echo count($b) <= 1 ? reset($b) : join(', ', array_slice($b, 0, -1)) . " & " . end($b);
strrpos finds the last occurrance of a specified string.
$str = '1, 2, 3';
$index = strrpos( $str, ',' );
if( $index !== FALSE )
$str[ $index ] = '&';
function fancy_implode($arr){
array_push($arr, implode(' and ', array_splice($arr, -2)));
return implode(', ', $arr);
}
I find this easier to read/understand and use
Does not modify the original array
Does not use regular expressions as those may fail if strings in the array contain commas, there could be a valid reason for that, something like this: array('Shirts (S, M, L)', 'Pants (72 x 37, 72 x 39)');
Delimiters don't have to be of the same length as with some of the other solutions
$bedroom_list = implode(", ", array_filter($bedroom_array));
$vars = $bedroom_list;
$last = strrchr($vars,",");
$last_ = str_replace(",","&",$last);
echo str_replace("$last","$last_",$vars);
<?php
$string = "3, 4, 5";
echo $string = preg_replace('/,( \d)$/', ' &\1', $string);
?>
Here's another way to do the replacement with a regular expression using a positive lookahead which doesn't require a backreference:
$bedroom_list = preg_replace('/,(?=[^,]*$)/',' &', implode(', ', $bedroom_array));
I have an array with strings like:
209#ext-local : SIP/209 State:Idle Watchers 2
208#ext-local : SIP/208 State:Unavailable Watchers 1
How can I echo the state for example Idle or Unavailable?
Thanks.
Using regex it will match any string containing letters and numbers.
$string = '209#ext-local : SIP/209 State:Idle Watchers 2';
preg_match("/State\:([A-Za-z0-9]+)/", $string, $results);
echo $results[1]; // Idle
strpos will search the string to see if it is contains the characters in that exact order.
strpos will not always work if the word idle or unavailable has the possibility to show up in any other way in the string.
You can use the php explode and parse the sting into an array of strings.
exp.
$string = "209#ext-local : SIP/209 State:Idle Watchers 2";
$string = explode(':', $string);
will give you ['209#ext-local ',' SIP/209 State','Idle Watchers 2']. Then if you explode the 3rd entry my ' ' you would get your answer.
$answer = explide(' ', $string[2]);
echo $answer[0];
Assuming your strings are all the same format, you can try splitting the string down using explode(), which returns an array of string, separated by a provided delimiter, like
foreach ($yourStrings as $s) {
$colonSplit = explode(":", $stringToSplit);
$nextStringToSplit = $colonSplit[2];
$spaceSplit = explode(" ", $nextStringToSplit);
$status = $spaceSplit[0];
echo $status;
}
May not be elegant but it should work.
Quick (and dirty) way. Assuming your array contains the full elements you listed above, the array element values do NOT contain 'idle' or 'unavailable' in any other capacity other than what you listed, and you just want to echo out the value and "is idle" or "is unavailable":
//$a being your array containing the values you listed above
foreach ($a as $status) {
if (strpos($status, "Idle") == true)
echo $status . " is idle";
elseif (strpos($status, "Unavailable") == true)
echo "$status" . " is unavailable";
}
how i separate a integer number from a given string with comma separate values. I have tried but not succeeded.
$Q22V=نعم1,لا2,نعم3,لا4,نعم5,لا6,نعم7,لا8,نعم9,لا10,نعم11,لا12
list($num,$letter) = preg_split('/(?<=\d)(?=[a-z]+)/i',$Q22v);
echo "- ".$letter."<span style=float:right>$ ".$num ."</span> <br>";
I want to display result like this
نعم 1
2 لا
3 لا
$Q22V = "نعم1,لا2,نعم3,لا4,نعم5,لا6,نعم7,لا8,نعم9,لا10,نعم11,لا12";
$items = explode(',', $Q22V);
foreach($items as $item){
preg_match('/([^0-9]*)([0-9]+)/', $item, $matches);
$letter = $matches[1];
$num = $matches[2];
echo $num . ' ' . $letter . '<br/>';
}
Explode the string on , to separate the string on commas.
Then use a regular expression to separate the numbers from the non-numbers.
$num will contain the number while $letter will contain the Arabic characters.
Output should be similar to your example output.
I have searched everywhere but can't find a solution that works for me.
I have the following:
$bedroom_array = array($studio, $one_bed, $two_bed, $three_bed, $four_bed);
For this example lets say:
$studio = '1';
$one_bed = '3';
$two_bed = '3';
I then use the implode function to put a comma in between all the values:
$bedroom_list = implode(", ", array_filter($bedroom_array));
echo $bedroom_list;
This then outputs:
1, 2, 3
What I want to do is find the last comma in the string and replace it with an &, so it would read:
1, 2 & 3
The string will not always be this long, it can be shorter or longer, e.g. 1, 2, 3, 4 and so on. I have looked into using substr but am not sure if this will work for what I need?
Pop off the last element, implode the rest together then stick the last one back on.
$bedroom_array = array('studio', 'one_bed', 'two_bed', 'three_bed', 'four_bed');
$last = array_pop($bedroom_array);
$string = count($bedroom_array) ? implode(", ", $bedroom_array) . " & " . $last : $last;
Convert & to the entity & if necessary.
if you have comma separated list of words you may use:
$keyword = "hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf, sdgdfg";
$keyword = preg_replace('/,([^,]*)$/', ' & \1', $keyword);
echo $keyword;
it will output:
hello, sadasd, sdfgdsfg,sadfsdafsfd, ssdf & sdgdfg
A one-liner alternative, that will work for any size array ($b = $bedroom_array):
echo count($b) <= 1 ? reset($b) : join(', ', array_slice($b, 0, -1)) . " & " . end($b);
strrpos finds the last occurrance of a specified string.
$str = '1, 2, 3';
$index = strrpos( $str, ',' );
if( $index !== FALSE )
$str[ $index ] = '&';
function fancy_implode($arr){
array_push($arr, implode(' and ', array_splice($arr, -2)));
return implode(', ', $arr);
}
I find this easier to read/understand and use
Does not modify the original array
Does not use regular expressions as those may fail if strings in the array contain commas, there could be a valid reason for that, something like this: array('Shirts (S, M, L)', 'Pants (72 x 37, 72 x 39)');
Delimiters don't have to be of the same length as with some of the other solutions
$bedroom_list = implode(", ", array_filter($bedroom_array));
$vars = $bedroom_list;
$last = strrchr($vars,",");
$last_ = str_replace(",","&",$last);
echo str_replace("$last","$last_",$vars);
<?php
$string = "3, 4, 5";
echo $string = preg_replace('/,( \d)$/', ' &\1', $string);
?>
Here's another way to do the replacement with a regular expression using a positive lookahead which doesn't require a backreference:
$bedroom_list = preg_replace('/,(?=[^,]*$)/',' &', implode(', ', $bedroom_array));
I've got a list of names separated by commas (they may contain other characters), or be empty, but generally looking like this:
NameA,NameB,NameC
I need to create a function to delete a name if its present in the list and which restores the comma separated structure.
eg: if NameA is to be deleted, I should end up with:
NameB,NameC
NOT
,NameB,NameC
Similarly for the rest.
This is what I came up with, is there a better solution?
$pieces = explode(",", $list);
$key=array_search($deleteuser, $pieces);
if(FALSE !== $key)
{
unset($pieces[$key]);
}
$list = implode(",", $pieces);
That should work pretty well. You may also be interested in PHP's fgetcsv function.
Doc: http://php.net/manual/en/function.fgetcsv.php
You could use the array_splice function to delete from the array. With offset = array_search($deleteuser, $pieces) and length = 1.
If your application data is so large that you are experiencing a crippling amount of lag, then you may have bigger issues that this snippet of code. It might be time to rethink your data storage and processing from the ground up.
In the meantime, here are some benchmarks when imploding a 5001 element array with commas, then using different techniques to remove value 4999. (speeds actually include the generation of the comma-separate string, but all benchmarks are identical in this regard)
explode() + array_search() + unset() + implode() (demo)System time: ~.009 - .011s
$pieces = explode(",", $list);
if (($key = array_search($deleteuser, $pieces)) !== false) {
unset($pieces[$key]);
}
echo implode(",", $pieces);
explode() + array_search() + array_splice() + implode() (demo)System time: ~.010 - .012s
$pieces = explode(",", $list);
if (($key = array_search($deleteuser, $pieces)) !== false) {
array_splice($pieces, $key, 1);
}
echo implode(",", $pieces);
explode() + foreach() + if() + unset() + break + implode() (demo)System time: ~.011 - .012s
$pieces = explode(",", $list);
foreach ($pieces as $key => $value) {
if ($value == $deleteuser) {
unset($pieces[$key]);
break;
}
}
echo implode(",", $pieces);
(note: if you remove the break, the loop can remove multiple occurrences of the needle)
explode() + array_diff() + implode() (demo)System time: ~.010 - .011s
$pieces = explode(",", $list);
$pieces = array_diff($pieces, [$deleteuser]);
echo implode(",", $pieces);
// or just: echo implode(',', array_diff(explode(",", $list), [$deleteuser]);
explode() + array_filter() + implode() (demo)System time: ~.010 - .013s
$pieces = explode(",", $list);
$pieces = array_filter($pieces, function($v) use ($deleteuser) {
return $v != $deleteuser;
});
echo implode(",", $pieces);
preg_quote() + preg_replace() (demo) (regex demo)System time: ~.007 - .010s
$needle = preg_quote($deleteuser, '/');
echo preg_replace('/,' . $needle . '(?=,|$)|^' . $needle . ',/', '', $list);
// if replacing only one value, declare preg_replace's 4th parameter as 1
Note that if you are using a delimiting character that has a special meaning to the regex engine (like +), then you will need to add a slash before it \+ to escape it and make it literal. This would make the pattern: '/\+' . $needle . '(?=\+|$)|^' . $needle . '\+/'
So while the regex-based snippet proved to be slightly (unnoticeably) faster for my arbitrarily conjured string length, you will need to make your own benchmarks to be sure which is the best performer for your application.
That said, and don't get me wrong I love regex, but the regular expression is going to be the easiest for developers to "get wrong" when it is time to modify the pattern AND I am confident that most devs will agree it has the worst overall comprehensibility.
You could also try a regular expression like this (maybe it can be optimized):
$search = 'NameB';
$list = 'NameA,NameB,NameC';
$list = preg_match('/(^' . $search . ',)|(,' . $search. ')/', '', $list);