0

i'm posting here for a problem i'm facing currently, i have an array where i have to filter values that is preceded by "!" and later count it, i tried array_filter but is of no use i would like to know if there is another way to filter values and separate values preceded by "!" i'm posting the array below with the code to save space

here is the code i have done so far:

$dat= Array
(
    [0] => Lorem
    [1] => ipsum
    [2] => dolor
    [3] => sit
    [4] => amet,
    [5] => consectetur
    [6] => adipiscing
    [7] => elit.
    [8] => <a
    [9] => class="btn
    [10] => btn-default"
    [11] => href="hash_sys.php?tag=bart">
    [12] => !bart
    [13] => </a>
    [14] => Quisque
    [15] => sapien
    [16] => velit,
    [17] => aliquet
    [18] => eget
    [19] => commodo
    [20] => nec,

    [21] => !qwerty
    [22] => auctor
    [23] => a
    [24] => sapien.
    [25] => Nam
    [26] => eu
    [27] => neque
    [28] => vulputate
    [29] => diam
    [30] => rhoncus
    [31] => faucibus.
    [32] => Curabitur
    [33] => quis
    [34] => varius
    [35] => libero.
    [36] => <a
    [37] => class="btn
    [38] => btn-default"
    [39] => href="hash_sys.php?tag=qwerty">
    [40] => !qwerty
    [41] => </a>
    [42] => Lorem.
)
 foreach($dat as $d){
                    if(strpos($d ,'!') !== FALSE ){
                 $d_p=""; 
            $i=0;
            $tag="";
                     $d2=count($dat);
                     while ($i<$d2) {

                         $d_w=$dat[$i];

                            $regex="/!+([a-zA-z0-9._-]+)/";
                            $regex1="/(?:\s|^)![A-Za-z0-9\-\.\_]+(?:\s|$)/";
                    $d1=preg_match_all($regex, $dat[$i],$output_preg,PREG_PATTERN_ORDER);


                       function check_regex($data){
                           if ($data=="/(?:\s|^)![A-Za-z0-9\-\.\_]+(?:\s|$)/")
                           {
                               return $data;
                           }

                           }


                       $d_p= array_column($dat,$output_preg[0][0],$regex1);

                       $d_f= array_filter($d_p,"check_regex" , ARRAY_FILTER_USE_BOTH);
                   $d_c= array_count_values($d_p);
                   print_r($d_p);
                   foreach ($d_c as $d2) {
                       if ($d2>1 ) {
                           $tag=1;

                       } 
                   }
                   $i++;
                     }

                     if($tag==1){
                        echo 1;
                        exit();
                    } else {
                    echo 0;
                }    
                     }
1
  • To make it easier for people to answer your question, try to only include the code where you are having a problem. Commented Oct 27, 2018 at 6:18

2 Answers 2

1

I did not read your code. It was just to massive for such a small question, so pardon me if I missed something.

I use preg_grep to filter out the values with ! in front.
If you then want a list of all without ! also you can use array_diff.

$arr = ["!qwerty","sapien", "!Bart", "!qwerty"];

$filtered = preg_grep("/^\!.*$/", $arr);

var_dump($filtered);
var_dump(array_diff($arr, $filtered));

https://3v4l.org/uj4Gb

The regex pattern will make sure the value starts with ! by using the ^ $ in the pattern and then anything is accepted with .*

Sign up to request clarification or add additional context in comments.

1 Comment

1

What I understand was:

  • Remove from array all elements that starts with !
  • Count the number of elements that was removed

Here is my working code

$dat= Array("Lorem","ipsum","dolor","sit","amet,","consectetur","adipiscing","elit.","&lt;a","class=&quot;btn"," btn-default&quot;"," href=&quot;hash_sys.php?tag=bart&quot;&gt;"," !bart"," &lt;/a&gt;"," Quisque"," sapien"," velit,"," aliquet"," eget"," commodo"," nec,",
    " !qwerty"," auctor"," a"," sapien."," Nam"," eu"," neque"," vulputate"," diam"," rhoncus"," faucibus."," Curabitur"," quis"," varius"," libero."," &lt;a"," class=&quot;btn"," btn-default&quot;"," href=&quot;hash_sys.php?tag=qwerty&quot;&gt;"," !qwerty"," &lt;/a&gt;"," Lorem.",
);
$filter = array();
foreach($dat as $d){
    if(substr($d,0,1) !== "!")
        $filter[] = $d;
}
echo "<h3>Original</h3>";
echo "<pre>"; var_dump($dat); echo "</pre>";
echo "<h3>Filtered</h3>";
echo "<pre>"; var_dump($filter); echo "</pre>";
echo "<h3>Count Difference</h3>";
echo "<pre>"; var_dump(sizeof($dat) - sizeof($filter)); echo "</pre>";

The substr checks if the first element of my string is !

if not starts with ! that add to the filter array

the filtered elements are the subtraction of size of dat and filter

2 Comments

I don't think you need the trim. If you look at the array it seems as an array from an explode on space
Agreed. Ill modify my answer. Anyways yours looks cleaner.

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.