What I am trying to do is hard to explain. I am trying to replace the a's to b's only inside the characters <<< and >>>
so this string... "abcd <<< this is a test >>> abcd" would turn into this... "abcd <<< this is b test >>> abcd"
and this string "123 <<< 123 abc >>" would turn into this... "123 <<< 123 bbc >>"
I hope I have made myself clear, that the contents inside and outside <<< and >>> are dynamic, but I would to keep it the same except for replacing the a's
Thanks
I'd say use preg_replace with the e modifier (which makes it execute the replacement as php code).
$str = preg_replace('/(<<<.*?>>>)/e', 'str_replace("a", "b", "\\1")', $str);
Thanks for your reply but it seems to delete what is inside the <<< and >>> for example returning "test <<<>>>test" is there a function that could split the string up into an array?
explode and preg_split.
Not intending to be annoying, but I did test that code. It should work. Did you copy & paste it?
I copy pasted... here is what I tried
<? $str = "abc<<<this is a test>>>abc"; $str = preg_replace('/(<<<.*?>>>)/e', 'str_replace("a", "b", "\\1")', $str); echo $str; ?>
and it outputs abc<<>>abc
I'm using php4, maybe it is a compatiblity issue 😕
try this:
<?php $str = "abc<<<this is a and this is a test>>>abc"; preg_match("/<<<(.*?)>>>/", $str, $match); $str = "<<<" . str_replace("a", "b", $match[1]) . ">>>"; echo $str; ?>
I tried the code as you gave it (well, I removed the extra space the forum added) on php 4.2.2, 4.3.10, and 5.0.3. All of them output "abc<<<this is b test>>>abc". Very odd.
The 'e' modifier wasn't available in php3. You did say you were using php4 though. Nothing else I can see has changed during 4.
EDIT: Okay, I've been testing via php command line. I just checked it via a web browser and I see "abc<<>>abc". Try viewing source on the page.
oh, that is cause the example I used confuses the browser into thinking it is a tag... my bad 😃
thanks for the continued help!