How does preg_match_all () process strings?

I am still learning a lot about PHP and changing strings is what interests me. I used preg_match before for things like checking email addresses or just searching for queries.

I just came from this post. What's wrong with my regex? and it was curious why the preg_match_all function produces 2 lines, 1 w / some of the characters devoid, and then another w / desired output.

From what I understand about the function, it is that it passes the string character to character using RegEx to evaluate what to do with it. Can this RegEx be structured in such a way as to bypass the first record of the array and simply produce the desired result?

and you do not need to go to another thread

$str = 'text^name1^Jony~text^secondname1^Smith~text^email1^example-
        free@wpdevelop.com~';

preg_match_all('/\^([^^]*?)\~/', $str, $newStr);

for($i=0;$i<count($newStr[0]);$i++)
{
    echo $newStr[0][$i].'<br>';
}

echo '<br><br><br>';

for($i=0;$i<count($newStr[1]);$i++)
{
    echo $newStr[1][$i].'<br>';
} 

This will lead to the conclusion

^ Jony ~
^ Smith ~
^example-free@wpdevelop.com~


Jony

example-free@wpdevelop.com

, . , , , .

,

+5
6

preg_match preg_match_all - " " - FULL-, . " ", / () .

regex /\^([^^]*?)\~/

^   Jony    ~
|     |     |
^  ([^^]*?) ~   -> $newstr[0] = ^Jony~
                -> $newstr[1] = Jony (due to the `()` capture group).
+2

RegEx , ?

. assertions. :

preg_match_all('/(?<=\^)[^^]*?(?=~)/', $str, $newStr);

:

Array
(
    [0] => Array
        (
            [0] => Jony
            [1] => Smith
            [2] => example-free@wpdevelop.com
        )

)
+2

manual, ( PREG_PATTERN_ORDER). $newStr , - ( ) ..

+1

preg_match_all , , preg_match_all(), /\ ^ ([^^] *?)\~/. . , :

$string = 'abcdefg';
preg_match_all('/ab(cd)e(fg)/', $string, $matches);

$matches

array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(7) "abcdefg"
  }
  [1]=>
  array(1) {
    [0]=>
    string(2) "cd"
  }
  [2]=>
  array(1) {
    [0]=>
    string(2) "fg"
  }
}

, "abcdefg". , "cd". , "fg".

+1

[0] , [1] - (, )... var_dump($newStr), , .

$str = 'text^name1^Jony~text^secondname1^Smith~text^email1^example-
        free@wpdevelop.com~';

preg_match_all('/\^([^^]*?)\~/', $str, $newStr);

$newStr = $newStr[1];
foreach($newStr as $key => $value)
{
    echo $value."\n"; 
}

... ( , )

Jony
Smith
example-
        free@wpdevelop.com
0

Whenever you have trouble presenting the preg_match_all function, you should use an evaluator like preg_match_all tester @ regextester.net

This shows the result in real time, and you can customize things like the order of the result, meta commands, offset capture, and more.

0
source

All Articles