# Search & Replace against collections

**URL:** <https://discourse.getcockpit.com/t/search-replace-against-collections/1167>\
**Category:** Support\
**Created:** [January 22, 2020, 10:00pm UTC](https://discourse.getcockpit.com/t/search-replace-against-collections/1167 "2020-01-22T22:00:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EAllison](https://avatars.discourse-cdn.com/v4/letter/e/b9e5f3/32.png) [@EAllison](https://discourse.getcockpit.com/u/EAllison)\
**Post date:** [January 22, 2020, 10:00pm UTC](https://discourse.getcockpit.com/t/search-replace-against-collections/1167/1 "2020-01-22T22:00:37Z")

</div>

We’ve been using cockpit in a test environment with some WYSIWIG fields. These fields contain absolute URLs which will need to be mass-replaced when we migrate. (e.g. “[https://test.domain.com/this\_thing](https://test.domain.com/this_thing)” will need to be replaced with “[https://www.domain.com/this\_thing](https://www.domain.com/this_thing)”) inside some Collections and Singletons.

What’s the best way to iterate through Collection entries and Singletons to do a search/replace?

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [January 23, 2020, 12:26pm UTC](https://discourse.getcockpit.com/t/search-replace-against-collections/1167/2 "2020-01-23T12:26:30Z")

</div>

simple approach: create a migrate script e.g. `migrate.php`.

```auto
<?php

include('{cockpit-folder}/bootstrap.php');

$collection = '{collectionname}';
$records = cockpit('collections')->find($collection); // get all entries

foreach ($records as $record) {
    $record['wysiwyg'] = str_replace('xyz', 'zyx', $record['wysiwyg']);

    cockpit('collections')->save($collection, $record);
}

```
