Hello All,

I've been knocking my head against the wall for several hours trying to figure out what's going on with a simple string replace. For the most part it is working beautifully, but if a string contains a similar but not exact match it still replaces the non match. I was under the impression that str_replace always looks for an exact match.

I have custom tags that are in strings of text that are structured like: [A_CUSTOM_HEADER] [A_CUSTOM_TITLE] and so on. They always start with "[A_CUSTOM" and end with a " ] ". In between the start and end can be just about anything. So my problem is if I have [A_CUSTOM] and [A_CUSTOM_TAG] in the same text str_replace seems to incorrectly replace all the [A_CUSTOM_TAG] with the [A_CUSTOM] content and the match never happens for the [A_CUSTOM_TAG] content.

For example:

$content = 'Here is some text and [A_CUSTOM].  Here is more text and [A_CUSTOM_TAG]'.
$content = str_replace('[A_CUSTOM]','I have a and custom',$content);
$content = str_replace('[A_CUSTOM_TAG]','I have a, custom and tag',$content);

So running that code The [A_CUSTOM_TAG] content never gets replaced correctly, it is replaced on the first pass with the [A_CUSTOM] replacement content. *

I hope this make sense I'm trying to explain it the best I can.

I tried using preg_replace() with \b to match the whole word but didn't get anything close to the respected results...haha

I'm sure I'm missing something simple...or at least I hope I am.

Thanks in advance!

    Sorry the issue was in a different part of the code. Marking solved

      Write a Reply...