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);
4
  • 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. Commented Apr 22, 2013 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? Commented Apr 22, 2013 at 18:29
  • You could cast your object using (array) notation before assigning your object to another variable. $collection = (array)$tag; Commented Apr 22, 2013 at 18:30
  • thanks! I've got it working, great help everyone, much appreciated Commented Apr 22, 2013 at 18:32

7 Answers 7

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]);
      }
}
Sign up to request clarification or add additional context in comments.

Comments

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 )

1 Comment

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
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;

Comments

1

Try this

<?php
foreach( $tags as $key => $val )
{
   if( !preg_grep( "/(?!Free_Course).*/", $tags ) ) {
      echo $val;
   }
}
?>

Comments

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)

Comments

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?

Comments

0
$cleanTags = array_diff($tags, preg_grep("/Free_Course.*/", $tags));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.