Consider the following string:
"This is a test string (Danwatt 1), with references to sources (SomeBook 3). There is also a reference here (Test 1), and another here (Another 2)."
What I am building is a function that highlights in-text references to other sources. These are found in the string by "(Source Page#)". Now, I can do the following:
$string=ereg_replace("(([A-Za-z ]+) ([1-9]+))","<a href=\"http://whatever.com/lookup.php?BOOK=\1&PAGENUM=\2\">\0</a>",$string);
And that works fine. The problem is that I need to validate each source to see if they actually exist. So instead of running the regexp on one line, I have to place it in a while (or other) loop.
while (ereg("(([A-Za-z ]+) ([1-9]+))",$string,$regs))
{
if (in_array(sources,$regs[1]))
//...do replace here
//... otherwise ignore it
}
The problem is that using this method, the string never gets removed. (note the \0 in the ereg_replace. \0 = the original string). I was wondering if anyone had some advice as to the best method to accomplish this. If you need any more details, please ask.
I had some ideas (like breaking the string up: find the first occurance (using ereg), find its position (using strstr), break off the string up to the end of that occurance, perform the replace on that section, then append that to the result string), but they all seemed like very slow / inefficient methods to accomplishing this taks.