I need to be able to replace a string with what would be wrapped in a sort of BBcode style element, but only if that string is not already wrapped in the element.
Example:
The fox runs really fast through Sudan, but also runs really fast through [country]Canada[/country]
Loopping through a list of all the countries I want to replace and wrap in this country brackets I tried this
$replace = preg_replace("#(?<!\[country(.)\])". preq_quote($country) ."(?!\[/country\])#", "[country]{$country}[/country]", $replace);
Expected output:
The fox runs really fast through [country]Sudan[country], but also runs really fast through [country]Canada[/country]
But Im actually getting
Actual output:
The fox runs really fast through [country]Sudan[country], but also runs really fast through [country][country]Canada[/country][/country]
If its already wrapped, I dont want to re-wrap it.