views:

272

answers:

2

Greetings,

Iv'e made a rapid search in the previous questions but did not find an adequate answer for my question.

I have create a function that finds words in an array library and replace these by links to the description of the word.

Example :

$words = array("ANTIM","APDIV","APVEG","ARCHE","ARFEU","ARMUR",
"ARSUP","ARTHE","ARTIL","ASSOM","ATTSU","BANQU","BARDE","BRICO",
"CAMOU","CHANC","CHAOM");

When I call my function, replace_text($someString). I search for these words in the text. Although these words are in several pages of my website and all need to link to there respective description. I made that function in order to automate the process and not write the links manually.

So when I call :

replace_text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultrices congue condimentum. Integer tempor aliquam ARFEU nisi at adipiscing. Vivamus ornare consequat metus. Nulla mollis lacus sit amet dolor rutrum sollicitudin. Maecenas sit amet nibh ut turpis bibendum rutrum et a tellus. Fusce a tortor a lacus feugiat gravida. Suspendisse elementum hendrerit urna ut viverra. Pellentesque ARCHE auctor, metus vitae lobortis vestibulum, justo ligula ultrices magna, vitae blandit mauris sapien eu velit. Nullam dapibus tristique orci, ac ullamcorper lectus venenatis ARTIL non. Nulla laoreet laoreet pretium.")

This will be replaced by :

"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis ultrices congue condimentum. Integer tempor aliquam <a href="somelink">ARFEU</a nisi at adipiscing. Vivamus ornare consequat metus. Nulla mollis lacus sit amet dolor rutrum sollicitudin. Maecenas sit amet nibh ut turpis bibendum rutrum et a tellus. Fusce a tortor a lacus feugiat gravida. Suspendisse elementum hendrerit urna ut viverra. Pellentesque <a href="somelink">ARCHE</a auctor, metus vitae lobortis vestibulum, justo ligula ultrices magna, vitae blandit mauris sapien eu velit. Nullam dapibus tristique orci, ac ullamcorper lectus venenatis <a href="somelink">ARTIL</a> non. Nulla laoreet laoreet pretium."

(The words found from the array were replaced with anchors.)

Finally, I want my function to be called but by searching within my whole pages (html code) automatically when loaded. I think this can be done with the domDocument part of PHP but I am not used to it, can you guide me please ?

+3  A: 

Sounds like you need ob_start().

You can create your function, lets say

 function call_back_addLinks($buffer){
    //do your magic and replace
    //words with links in $buffer
    //then return the new String
 }

Then:

 ob_start("call_back_addLinks");

Then your normal PHP code to generate normal content. That is, your entire page here.

Then:

 ob_end_flush();

The link has a simple example.

Vincent Ramdhanie
I'll try this method... although I think I found an easy way out. I may use the following :$content = file_get_contents('./my-content-page.html', FILE_USE_INCLUDE_PATH);echo my_replace_text_function($content);This seems to be working correctly... Ill test and inform you !
wiooz
A: 

I finally got it to work correctly. Thanks for your guide lines!

wiooz