I'm new to PHP and have just learned how to use regular expressions earlier today. I'm trying to fetch a string, match a desired pattern within the string, analyze the match, and according to the results of the analysis, replace the match with some newly-concocted words. I know this involves using regular expressions and probably has some shortcut function like preg_replace or something, but I haven't been able to figure it out yet.
Here's an example of the string before I process it:
Code: Select all
<body>
Here's a sweet picture!
<< monkeyfish >>
Here's another sweet picture!
<< meowplow >>
</body>
Here's an example of the string after it's processed:
Code: Select all
<body>
Here's a sweet picture!
<img src="monkeyfish.jpg">
Here's another sweet picture!
<img src="meowplow.jpg">
</body>
Basically, I want to find the words between "<<" and ">>", isolate the words, add text before and after the words, then replace "<< words >>" with my new creation. What's the best way to accomplish this?