2

I have an array of strings, and want to remove all elements from the array which contain the word "Free_Course"

the array holds strings such as "Free_Course_123", "Free_Course_124", "Other_Course_123", Other_Course_1234 etc..

I just want to loop through the array, and remove all containing "Free_Course", so this would get rid of the first 2 elements listed above.

I've tried using this preg_grep funciton, but had no success as of yet:

$cleanTags= preg_grep("/^Free_Course*/", $tags);
Charles
  • 50,943
  • 13
  • 104
  • 142
gray
  • 798
  • 18
  • 31
  • 1
    `^` at the start of a regex pattern is NOT an inversion. it's the "start of string" anchor. It's an inversion only when at the start of a character class, e.g. `[^xyz]` (anything except x/y/z). your regex as written will simply return an array the very things you're trying to remove - all entries that start with `Free_Course`. – Marc B Apr 22 '13 at 18:25
  • I keep getting this error: Object of class stdClass could not be converted to string..maybe my array aren't string but objects? Any idea? – gray Apr 22 '13 at 18:29
  • You could cast your object using (array) notation before assigning your object to another variable. $collection = (array)$tag; – Th. Ma. Apr 22 '13 at 18:30
  • thanks! I've got it working, great help everyone, much appreciated – gray Apr 22 '13 at 18:32

7 Answers7

3

You could use native PHP function strpos (http://php.net/manual/fr/function.strpos.php) to identify the values in your array which should be removed:

<?php

foreach ($tags as $index => $tag) {
      if (strpos($tag, 'Free_Course') !== false) {
          unset($tags[$index]);
      }
}
Th. Ma.
  • 9,432
  • 5
  • 31
  • 46
2

Have you tried

$tags = array(
    "Free_Course_123", 
    "Free_Course_124", 
    "Other_Course_123", 
    "Other_Course_1234"
);
$cleanTags= preg_grep("/Free_Course/", $tags, PREG_GREP_INVERT);
print_r($cleanTags);

? The last attribute inverts the filter function, see the manual

I have tested the result, it should work since PHP 4.2.0

Array ( [2] => Other_Course_123 [3] => Other_Course_1234 )
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • I keep getting this error: Object of class stdClass could not be converted to string..maybe my array aren't string but objects? I'm pretty new with this, thanks though – gray Apr 22 '13 at 18:27
1

You could do a preg_match, and only return those that do not match.

foreach($array as $key => $value)
    if(!preg_match("/Free_Course/i", $value))
        echo $value . '<br>';

Or to create a new array...

foreach($array as $key => $value)
    if(!preg_match("/Free_Course/i", $value))
        $new_array[] = $value;
1

Try this

<?php
foreach( $tags as $key => $val )
{
   if( !preg_grep( "/(?!Free_Course).*/", $tags ) ) {
      echo $val;
   }
}
?>
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
0

Using array_walk:

$arr = array(
    "Free_Course_123", 
    "Free_Course_124", 
    "Other_Course_123", 
    "Other_Course_1234"
);

$arr = array_filter($arr, function($e){
    return strpos($e, 'Free_Course') === false;
});

(edit: i've misunderstood the question)

0
$cleanTags = array_diff($tags, preg_grep("/Free_Course.*/", $tags));
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aleksandr Belkin
  • 412
  • 5
  • 17
0

"^" only means negation when used at the beginning of a character class list, ie "[^a-zA-Z0-9]+ means "one or more characters that are NOT alphanumeric characters".

If you use "^" at the beginning of a (perl) regular expression it means "The follow must be at the beginning of the line". So "/^Free_Course*/" will match any string that begins with "zero or more instances of the text 'Free_Course'", which is not what you want.

I would use Thierry Marianne's solution.

If you're wondering, "how do I negate a word with regex then?", see this thread:

Regular expression to match a line that doesn't contain a word?

Community
  • 1
  • 1
Drew G
  • 283
  • 5
  • 15