PDA

Click to See Complete Forum and Search --> : the first instence of string from array A in string B


Matt_T_hat
Oct 11th, 2006, 02:22 AM
I wish to write a process that will automatically act on a block of text.

My first idea was to take the list of words and phrases and build a regex part like this [word1|word2|etcetc...] which would allow me to convert each word to a hyperlink.

However if more than one instence of any word is made into a link this will cause problems for the indexer which will list the block of text twice...

So I need to find only the first example of each word.

However, I need to be as fast as possible as each such lot of code slows down the text addition process and it is already a bit bogged.

Q. So what is the fastest way to do this?

Q. Is there a reasonably efficient way to do this and detect for user added links? (does it make any difference that the links could come in BBish, HTML or custom tag markup?)

Any clues at all would be helpfull at this stage.

Matt_T_hat
Dec 8th, 2007, 09:06 AM
I'm doing this in php. The idea being to hyper link the first instance of any word for the list in a body of HTML that might contain unprocessed BB/wiki/custom markup.

I need to be able to check I am not wrapping an a-href arround markup or existing a-href...

dclamp
Dec 8th, 2007, 11:47 PM
you could try just using explode, and then loop through the array.


$string = "word1|word2|word3";
$words = explode('|', $string);
$i=0;
while($i < count($words)) {
echo "<a href='whatever.php?keyword={$words[$i]}'>{$words[$i]}</a>";
$i++;
}


that code will loop through $string and print a link for all of the words in the array separated by the |

EDIT:
Here is a working example: http://www.subsoft.net/vbf/array_test.php