Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using preg_replace on an array

I have a relatively large array of elements which I want to search for a string and replace any matches. I'm currently trying to do this using preg_replace and regular expressions:

preg_replace("/\d?\dIPT\.\w/", "IPT", $array);

I want to get all values which match either 00IPT.A or 0IPT.A (with 0 representing any numerical character and A representing any letter) and replace them with IPT. However, I'm getting array to string conversion notices. Is there any way to get preg_replace to accept an array data source? If not, is there any other way I could achieve this?

The documentation says that preg_replace should be able to accept array sources — this is the reason I'm asking.

The string or an array with strings to search and replace. If subject is an array, then the search and replace is performed on every entry of subject, and the return value is an array as well.

The array is multidimensional if that helps (has multiple arrays under one main array).

like image 759
Carey Avatar asked Mar 21 '14 08:03

Carey


People also ask

What is the difference between Str_replace and Preg_replace?

str_replace replaces a specific occurrence of a string, for instance "foo" will only match and replace that: "foo". preg_replace will do regular expression matching, for instance "/f. {2}/" will match and replace "foo", but also "fey", "fir", "fox", "f12", etc.

What is use of Preg_replace in PHP?

The preg_replace() function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings.

Which function is used to replace in pattern in string?

String.prototype.replace() The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function called for each match.

What are Regular Expressions in PHP?

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character, or a more complicated pattern.


1 Answers

preg_replace doesn't modify in place. To permanently modify $array, you simply need to assign the result of preg_replace to it:

$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);

works for me.

$array = array('00IPT.A', '0IPT.A');
$array = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $array);
var_dump($array);
// output: array(2) { [0]=> string(3) "IPT" [1]=> string(3) "IPT" }

Note: the \d{1,2} means one or two digits.

If you want to do this to a two-dimensional array, you need to loop through the first dimension:

$array = array( array('00IPT.A', 'notmatch'), array('neither', '0IPT.A') );    
foreach ($array as &$row) {
    $row = preg_replace("/\d{1,2}IPT\.\w/", "IPT", $row);
}
var_dump($array);

output:

array(2) { 
    [0]=> array(2) { 
        [0]=> string(3) "IPT" 
        [1]=> string(8) "notmatch" 
    } 
    [1]=> &array(2) { 
        [0]=> string(7) "neither" 
        [1]=> string(3) "IPT" 
    } 
}

Note that you have to loop through each row by reference (&$row) otherwise the original array will not be modified.

like image 88
Tom Fenech Avatar answered Oct 05 '22 20:10

Tom Fenech