array search and replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

array search and replace

Post by pedroz »

with the following array

$array = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attachment/d.htm');

How can i replace in the array all '/attachment/' for '/attach/' and get...?

$array = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attach/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attach/d.htm');
Bind
Forum Contributor
Posts: 102
Joined: Wed Feb 03, 2010 1:22 am

Re: array search and replace

Post by Bind »

PHP's str_replace() function

str_replace ( $needle , $replacement_needle , $haystack [, count ] )

example:

Code: Select all

<?php
$paths = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attachment/d.htm');
$paths = str_replace('attachment','attach',$paths);
?>
NOTE: in your code, $array is an illegal variable name in PHP as it's a reserved word, so when you try it, name your array something other than $array.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: array search and replace

Post by pedroz »

Many thanks! You are right, should not call it $array.
However, I did a mistake asking for the replacement. I need the following...

$Sarray = ([1zxc] => 'a/abc/m.htm', [2zac] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4zac] => 'k/attachment/d.htm');

replacing za for TW

$Sarray = ([1zxc] => 'a/abc/m.htm', [2TWc] => 'b/attachment/d.htm', [3c] => 'd/dsds/hre23.htm', [4TWc] => 'k/attachment/d.htm')

Presume I have to unset the 2zac and 4zac... How is the fastest way to do it?
Thanks again...
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: array search and replace

Post by buckit »

so you need to change the key and not the value?

This should work... but I would recommend using this as just an example to learn how to write a better one :)

Code: Select all

function findReplaceArrayKeyName($hayStack, $find, $replace){
	foreach($hayStack as $k=>$v){
		$k = str_replace($find,$replace,$k);
		$newArray[$k] = $v;
	}
	return $newArray;
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array search and replace

Post by AbraCadaver »

Another example, only because I don't like loops if I can get away from them (even though that's a standard way of modifying arrays) and I like array functions. :)

Code: Select all

$Sarray = array_combine(str_replace('za', 'TW', array_keys($Sarray)), array_values($Sarray));
Also, in PHP reserved words CAN be used as variables, just NOT constants, class names, function or method names. I use $array in examples, but in production code you should use something descriptive. In this case $paths, $pages, $attachments or whatever makes sense.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Re: array search and replace

Post by pedroz »

Code: Select all

$Sarray = array ('/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2] [()/trackback/?$', 'xyz/atttachment/test' => 'index.php?attachment=$matches[1]&cpage=$matches[2] [()/trackback/?$');

$Sarray  = array_combine(str_replace('/attachment/', '/attach/', array_keys($Sarray)), array_values($Sarray));
print_r($Sarray);
why doesn't it works for the second element of the array? it only replace the first one...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: array search and replace

Post by AbraCadaver »

My example was to replace the keys. Use Bind's example to replace attachment in the values.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply