SM9 SM9 - 6 months ago 18
PHP Question

preg_replace that ignores a new line?

Sorry for the vague title, but basically I have no idea when it comes to regex, but I need a match for the following:

Example

https://fragdeluxe.com/api/csgo_schema.php

Basically, I want to ignore all the spaces and new lines, and be able to replace "items" with "items2" only if a { is followed.

Any help would be greatly appreciated.

Edit: I would need it to replace first occurrence only, The idea is to stop any further occurrences overwriting it when I convert it into an array.

The original file is here: https://raw.githubusercontent.com/SteamDatabase/GameTracking/master/csgo/csgo/scripts/items/items_game.txt

Thanks.

Answer

How about:

$str = preg_replace('/"items"(?=\s+\{)/', '"items2"', $str, 1);

\s stands for any white space included linebreak.

Comments