In php, there is an array of variables having 4 words separated by ";" For example:

$a[0] = "xx;yy;zz;kk"; $a[1] = "nn;yy;zz;kk"; $a[2] = "xx;yy;aa;kk"; 

How can I find the string "xx;yy;любой_значение;kk" from this array "xx;yy;любой_значение;kk" instead of zz, let it be any value but the main thing is to be in the form: "xx;yy;zz;kk" or "xx;yy;любой_значение;kk" ?

    2 answers 2

    Using regular expressions:

     $str = 'xx;yy;zz;kk'; $str = preg_replace('/((?:.+;){2}).+;(.+)/', '$1любой_значение;$2', $str); 

    Using explode + implode:

     $arr = explode(';', $str); $arr[2] = 'любой_значение'; $str = implode(';', $str); 

      You can do the following:

       <?php $a[0] = "xx;yy;zz;kk"; $a[1] = "nn;yy;zz;kk"; $a[2] = "xx;yy;aa;kk"; $regexp = "/xx;yy;[^;]+;kk/"; foreach($a as $element) { if(preg_match($regexp, $element)) { echo $element."<br />"; } }