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');
array search and replace
Moderator: General Moderators
Re: array search and replace
PHP's str_replace() function
str_replace ( $needle , $replacement_needle , $haystack [, count ] )
example:
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.
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);
?>Re: array search and replace
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...
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...
Re: array search and replace
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
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;
}
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: array search and replace
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.
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.
Code: Select all
$Sarray = array_combine(str_replace('za', 'TW', array_keys($Sarray)), array_values($Sarray));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.
Re: array search and replace
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);- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: array search and replace
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.