Here's my users list
james,john,mark,luke,
james,john,mark,luke,mary,david
I want to remove the repeated part and retain the remaining.
So here's my code
$old = 'james,john,mark,luke,';
$users = 'james,john,mark,luke,mary,david';
// Replace , with |
$expr = rtrim(preg_replace('/\,/', '|', $old), '|');
$newstr = preg_replace("/$expr/i", '', $users);
echo $newstr;
Output
,,,,mary,david
I want
mary,david,
A solution, based on your code using trim:
$old = 'james,john,mark,luke,';
$users = 'james,john,mark,luke,mary,david';
$expr = rtrim(preg_replace('/\,/', '|', $old), '|');
$newstr = preg_replace("/$expr/i", '', $users);
echo trim($newstr, ',');
Demo: https://3v4l.org/W3XKe
A better solution using explode, array_diff and implode:
$old = 'james,john,mark,luke,';
$users = 'james,john,mark,luke,mary,david';
$old_items = explode(',', $old);
$users_items = explode(',', $users);
echo implode(array_diff($users_items, $old_items), ',');
Demo: https://3v4l.org/tnt9F
Can't test it right now, but wouldn't it work simply like that?
$old = 'james,john,mark,luke,';
$users = 'james,john,mark,luke,mary,david';
echo preg_replace("/$old/", "", $users); //should print "mary,david"
Related
I have done a lot of search. Almost every answer is about array. In my situation, I want to remove the same number.
<?php
$term="1,2,3.4";
$n='2';
//I want to remove 2 when the $n equal one number of $term.
// echo out like 1,3,4
?>
This should work for you:
(I assume that 1,2,3.4 the dot only was a typo)
<?php
$term = "1,2,3,4";
$n = "2";
$arr = explode(",", $term);
if(($key = array_search($n, $arr)) !== FALSE)
//^^^ to make sure when '$n' is not found in the array, that it doesn't unset the first array element
unset($arr[$key]);
echo implode(",", $arr);
?>
Output:
1,3,4
I have done a lot of search.
And you didn't find str_replace() function?
$string = '1,2,3,4,5,6;'
$n = '2';
$string = str_replace($n, '', $string);
$string = str_replace(',,', ',', $string);
No need to waste memory with arrays or using regular expressions.
$term = "1,2,3,4";
$n = 2;
$term_array = explode(',', $term);
$n_key = array_search($n, $term_array);
if ($n_key !== false)
unset($term_array[$n_key]);
$new_terms = implode(',', $term_array);
Ouput :
1,3,4
Hope this helps
$n = '2';
$str = '2,1,2,3,4,5,6,2';
$pattern = '/,2|,2,|2,/';
$after = preg_replace($pattern, '', $str);
echo $after
Ouput
1,3,4,5,6
mybe it's more simple
How can I convert to uppercase for the following example :
title-title-title
Result should be:
Title-Title-Title
I tried with ucwords but it converts like this: Title-title-title
I currently have this:
echo $title = ($this->session->userdata('head_title') != '' ? $this->session->userdata('head_title'):'Our Home Page');
In this particular string example, you could explode the strings first, use that function ucfirst() and apply to all exploded strings, then put them back together again:
$string = 'title-title-title';
$strings = implode('-', array_map('ucfirst', explode('-', $string)));
echo $strings;
Should be fairly straightforward on applying this:
$title = '';
if($this->session->userdata('head_title') != '') {
$raw_title = $this->session->userdata('head_title'); // title-title-title
$title = implode('-', array_map('ucfirst', explode('-', $raw_title)));
} else {
$title = 'Our Home Page';
}
echo $title;
echo str_replace(" ","-",ucwords(str_replace("-"," ","title-title-title")));
Fiddle
Output:
Title-Title-Title
Demo
Not as swift as Ghost's but a touch more readable for beginners to see what's happening.
//break words on delimiter
$arr = explode("-", $string);
//capitalize first word only
$ord = array_map('ucfirst', $arr);
//rebuild the string
echo implode("-", $ord);
The array_map() applies callback to the elements of the given array. Internally, it traverses through the elements in our word-filled array $arr and applies the function ucfirst() to each of them. Saves you couple of lines.
Edit #2
This isn't working for the new information added to op, as there is an answer this won't be updated to reflect that.
Edit #1
$var = "title-title-title";
$var = str_replace (" ", "_", ucwords (str_replace (" ", "_", $var));
Old, non-working
$var = "title-title-title";
$var = implode("-", ucwords (explode("-", $var)));
try the following:
$str='title-title-title';
$s='';
foreach(explode('-',$str) as $si){
$s.= ($s ? "-":"").ucfirst($si);
}
$s should be Title-Title-Title at this point
This works to combine ucwords and str_replace if I only need to replace one character:
echo str_replace('_', ' ', ucwords($cname));
This works to replace words/characters:
$search_name = array('_', ',', 'neckline', 'skirts', 'skirt','pants', 'neckline_size', 'sleeves', 'pants_length');
$replace_name = array(' ', ', ', '');
$rows = str_replace($search_name, $replace_name, $row);
echo $rows['silhouettes_like'];
But if I try combining them by adding this, it echo's "Array" instead of the value
$style_names = ucwords($rows);
Looks like you want the array_map function to map ucwords to each row value
$rows = array_map('ucwords', $rows);
*strong text*i have a string like that "x", "x,y" , "x,y,h"
i want to user preg replace to remove the commas inside the double qutations and return the string as
"x", "xy" , "xyh"
You can just use a regular replace.
$mystring = str_replace(",", "", $mystring);
You dont need preg_replace() here and whereever possible you should trying to avoid it
$string = str_replace(',', '', $string);
This would work fine: http://codepad.org/lq7I5wkd
<?php
$myStr = '"x", "x,y" , "x,y,h"';
$chunks = preg_split("/\"[\s]*[,][\s]*\"/", $myStr);
for($i=0;$i<count($chunks);$i++)
$chunks[$i] = str_replace(",","",$chunks[$i]);
echo implode('","',$chunks);
?>
I use the following, which I've found is generally faster than regexp for this type of replacement
$string = '"x", "x,y" , "x,y,h"';
$temp = explode('"',$string);
$i = true;
foreach($temp as &$value) {
// Only replace in alternating array entries, because these are the entries inside the quotes
if ($i = !$i) {
$value = str_replace(',', '', $value);
}
}
unset($value);
// Then rebuild the original string
$string = implode('"',$temp);
$data = "google,bing,searchengine,seo,search";
$exp = explode(",",$data);
$filtr = array("google","bing");
$fdat = str_replace($filtr,"",$data);
$clean = implode(",",$fdat);
echo $clean;
this gives out put ,,searchengine,seo,search
How can I get rid of first two blank commas?
Better get the difference of your splitted arrays $exp minus $filtr:
$clean = implode(',', array_diff($exp, $filtr));
This will also avoid the chance that you will only remove a substring of another word like when removing car from bike,car,carpet should result in bike,carpet and not in bike,pet.
And if you want to allow whitespace before and after each word, consider using trim and preg_split:
$exp = preg_split('/\s*,\s*/', trim($data));
trim will remove any preceding and trailing whitespace and the pattern for preg_split allows whitespace surrounding the comma too.
I'm getting an error when trying this code you did. You can use the following to remove google & bing (that are in an array) from a comma separated string:
$data = "google,bing,searchengine,seo,search";
$exp = explode(",",$data);
$filtr = array("google","bing");
$diff = array_diff($exp, $filtr);
$clean = implode(",",$diff);
echo $clean;
Your piece of code could also look like this:
$data = "google,bing,searchengine,seo,search";
$exp = explode(",",$data);
$filtr = array("google","bing");
foreach ($exp as $key => $item) {
if (in_array($key, $filtr)) unset($exp[$key]);
}
$clean = implode(",",$exp);
echo $clean;
Its useful when there is few items in $data. For big arrays it would need optimizing.
You would be better if checking the value within a loop like so:
$data = "google,bing,searchengine,seo,search";
$exp = explode(",",$data);
$filtr = array("google","bing");
foreach($exp as $k => $v)
{
if(in_array($v,$filtr))
{
unset($ext[$k]);
}
}
$clean = implode(",",$ext);
echo $clean;