Remove comma inside quoted string in a comma separated list - php

I have a string
$str = '1,2,3,4,5,"6,000",7,8,9';
How can I clean it up to:
'1,2,3,4,5,6000,7,8,9'

http://php.net/str-getcsv
$items = str_getcsv($str);
$items = array_map (
function ($item) { str_replace(',', '', $item); },
$items;
);
Now if you like you can merge them together again
$str = implode(',', $items);

Try something like this (for PHP < 5.3):
$dst = array();
$str = '1,2,3,4,5,"6,000",7,8,9';
$state = false;
$buffer = '';
for ($i = 0, $lim = strlen($str); $i < $lim; $i += 1) {
$char = $str[$i];
switch ($char) {
case ',':
if (!$state) {
$dst[] = $buffer;
$buffer = '';
}
break;
case '"':
$state = !$state;
break;
default:
$buffer .= $char;
}
}
$dst[] = $buffer;
print_r($dst);

Use this:
$str = '1,2,3,4,5,"6,000",7,8,9';
$pattern='/"(\d+),(\d+)"/';
$replacement='${1}$2';
echo preg_replace($pattern, $replacement, $str );
Thanks:-)

<?php
$str = '1,2,3,4,5,"6,000",7,8,9';
echo $str;
echo str_replace('"','',$str);
?>

Related

PHP merge two Paths - relalative absolute

Anybody know if there is a way to merge to Paths easily?
/www/htdocs/v450687/server/jobs/bodymind/uploads
uploads/videoscontent/1/
to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/
/www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent
uploads/videoscontent/1/snips
to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/snips
/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1
1/1/snips
to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/1/snips
Solution:
built in function to combine overlapping string sequences in php?
echo replaceOverlap("abxcdex", "xcdexfg"); //Result: abxcdexfg
function findOverlap($str1, $str2){
$return = array();
$sl1 = strlen($str1);
$sl2 = strlen($str2);
$max = $sl1>$sl2?$sl2:$sl1;
$i=1;
while($i<=$max){
$s1 = substr($str1, -$i);
$s2 = substr($str2, 0, $i);
if($s1 == $s2){
$return[] = $s1;
}
$i++;
}
if(!empty($return)){
return $return;
}
return false;
}
function replaceOverlap($str1, $str2, $length = "long"){
if($overlap = findOverlap($str1, $str2)){
switch($length){
case "short":
$overlap = $overlap[0];
break;
case "long":
default:
$overlap = $overlap[count($overlap)-1];
break;
}
$str1 = substr($str1, 0, -strlen($overlap));
$str2 = substr($str2, strlen($overlap));
return $str1.$overlap.$str2;
}
return false;
}
As far as I know, there is no built-in method for this. Here is my approach:
Explode the two paths into an array
Merge them together
Remove duplicates.
Here is an example:
$path_1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path_2 = 'uploads/videoscontent/1/';
echo implode('/', array_unique(array_merge(explode('/', $path_1), explode('/', $path_2)), SORT_REGULAR));
Output: /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1
You have to explode paths into arrays, merge them, and remove duplicates. You can do it in different ways, here are some examples:
$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path2 = 'uploads/videoscontent/1/';
print_r(pathToArray($path1, $path2));
function pathToArray($path1, $path2){
foreach(explode('/', $path1) as $part){
$output1[] = $part;
}
foreach(explode('/', $path2) as $part){
$output2[] = $part;
}
$output = array_merge($output1, $output2);
$output = array_unique($output);
$output = implode("/",$output);
return $output;
}
Or
$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path2 = 'uploads/videoscontent/1/';
echo implode('/', array_unique(array_merge(explode('/', $path_1), explode('/', $path_2)), SORT_REGULAR));
UPDATE:
As I see you have updated your question, so I develop my answer. In this case to fix this, all you need to do is to use array_unique() for each array instead.
$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1';
$path2 = '1/1/snips';
print_r(pathToArray($path1, $path2));
function pathToArray($path1, $path2){
foreach(explode('/', $path1) as $part){
$output1[] = $part;
}
foreach(explode('/', $path2) as $part){
$output2[] = $part;
}
$output1 = array_unique($output1);
$output2 = array_unique($output2);
$output = array_merge($output1, $output2);
//$output = array_unique($output);
$output = implode("/",$output);
return $output;
}

Explode a string in php excluding the ', ' within braces

I have a string of this-
$str = "field1.id as field1,
DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2,
field3.name as field3";
Need to explode this into array with , as this:
$requiredArray = array(
0 => field1.id as field1,
1 => DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2
2 => field3.name as field3
);
I've tried with explode but it doesn't works:
$requiredArray = explode(', ', $str);
// doesn't work as "DATE_SUB(field2, INTERVAL ..." also gets exploded
Any trick/ideas?
Please try this
$str = "field1.id as field1,
DATE_SUB(field2, INTERVAL (DAYOFMONTH(field2)-1) DAY) as field2,
field3.name as field3";
$buffer = '';
$stack = array();
$depth = 0;
$len = strlen($str);
for ($i=0; $i<$len; $i++) {
$char = $str[$i];
switch ($char) {
case '(':
$depth++;
break;
case ',':
if (!$depth) {
if ($buffer !== '') {
$stack[] = $buffer;
$buffer = '';
}
continue 2;
}
break;
case ' ':
if (!$depth) {
$buffer .= ' ';
continue 2;
}
break;
case ')':
if ($depth) {
$depth--;
} else {
$stack[] = $buffer.$char;
$buffer = '';
continue 2;
}
break;
}
$buffer .= $char;
}
if ($buffer !== '') {
$stack[] = $buffer;
}
echo "<pre>";
print_r($stack);
echo "</pre>";
Try this :
preg_match_all('~(.*)[^(],?~', $str, $matches);

how can i use python's like "For loop" in php to reverse string?

Here is python code:
def is_palindrome(s):
return revers(s) == s
def revers(s):
ret = ''
for ch in s:
ret = ch + ret
return ret
print is_palindrome('RACECAR')
# that will print true
when i convert that function to php.
function is_palindrome($string){
if (strrev($string) == $string) return true;
return false;
}
$word = "RACECAR";
var_dump(is_palindrome($word));
// true
Both functions works fine but, how can i revers string with php in loop ??
$string = str_split(hello);
$output = '';
foreach($string as $c){
$output .= $c;
}
print $output;
// output
hello
//i did this,
that's work find but is there any way to do that in better way ?
$string = "hello";
$lent = strlen($string);
$ret = '';
for($i = $lent; ($i > 0) or ($i == 0); $i--)
{
$ret .= $string[$i];
#$lent = $lent - 1;
}
print $output;
//output
olleh
Replace
$output .= $c;
with
$output = $c . $output;
Can't be shorter I guess. With a loop :)
$word = "Hello";
$result = '';
foreach($word as $letter)
$result = $letter . $result;
echo $result;
I don't try that code, but I think that it should work:
$string = "hello";
$output = "";
$arr = array_reverse(str_split($string)); // Transform "" to [] and then reverse => ["o","l","l,"e","h"]
foreach($arr as $char) {
$output .= $char;
}
echo $output;
Another way:
$string = "hello";
$output = "";
for($i = strlen($string); $i >= 0; $i--) {
$output .= substr($string, $i, 1);
}
echo $output;
strrev() is a function that reverses a string in PHP.
http://php.net/manual/en/function.strrev.php
$s = "foobar";
echo strrev($s); //raboof
If you want to check if a word is a palindrome:
function is_palindrome($word){ return strrev($word) == $word }
$s = "RACECAR";
echo $s." is ".((is_palindrome($s))?"":"NOT ")."a palindrome";

Replace string in php

I have this string:
525,294,475,215,365,745
and i need to remove 475..and comma.
and if i need to remove first number i need to remove also the next comma, also for last.
I can i do?
a regular expression?
thx
$newStr = str_replace(',475', '525,294,475,215,365,745');
Or the less error prone way:
$new = array();
$pieces = explode(',', '525,294,475,215,365,745');
foreach ($pieces as $piece) {
if ($piece != 475) {
$new[] = $piece;
}
}
$newStr = implode(',', $new);
Here's a regular expression:
$s = "525,294,475,215,365,745";
$s = preg_replace(',?475', '', $s);
$data = "525,294,475,215,365,745";
$parts = explode(',', $data);
for ($i = 0; $i < count($parts); $i++) {
if ($parts[$i] == 475) {
unset($parts[$i]);
}
}
$newdata = join(',', $parts);
<?php
$bigString = "525,294,475,215,365,745";
$pos = strpos($bigString, ",");
while($pos != false) {
$newString .= substr($bigString, 0, $pos);
$bigString = substr($bigString, $pos + 1);
$pos = strpos($bigString, ",");
}
echo $newString;
?>
function filterValue($index, &$a)
{
$key = array_search($index, $a);
if ($key != false) {
unset($a[$key]);
}
}
// Original data
$data = "525,294,475,215,365,745";
$data = explode(',', $data);
filterValue('475', $data);
$output = implode(',', $data);

php find number of occurances (number of times)

can someone help with this?
I need the following function to do this...
$x = 'AAAABAA';
$x2 = 'ABBBAA';
function foo($str){
//do something here....
return $str;
}
$x3 = foo($x); //output: A4B1A2
$x4 = foo($x2);//output: A1B3A2
substr_count is your friend
or rather this
function foo($str) {
$out = '';
preg_match_all('~(.)\1*~', $str, $m, PREG_SET_ORDER);
foreach($m as $p) $out .= $p[1] . strlen($p[0]);
return $out;
}
function foo($str) {
$s = str_split($str);
if (sizeof($s) > 0) {
$current = $s[0];
$output = $current;
$count = 0;
foreach ($s as $char) {
if ($char != $current ) {
$output .= $count;
$current = $char;
$output .= $current;
$count = 1;
}
else {
$count++;
}
}
$output .= $count;
return $output;
}
else
return "";
}

Categories