Jump to content

change words to links in an html content


asmith

Recommended Posts

Hey guys

 

I have a list of words, that I want to match them in a paragraph, If any word in that paragraph were the same as list, then it turn that word to a link. It is pretty simple with str_replace().

 

Now the thing is, The paragraph I have is not raw text.  for example :  for making the word "php" to link : 

 

This page contains <p class="php">some php information here</a>

 

str_replace will replace the class="php" to  class="<a href=link>php</a>" , which will mess up the page. If I use strip_tags first, Then I will lost the html content. 

 

Is there anyway I could remove tags, change the words to links, then I put the tags back?

Or any better idea?

 

thanks

Link to comment
Share on other sites

You need to use regular expressions, I find these quite complicated, but there are a number of good sites out there with some that u can use.

 

The function you would need is preg_replace() / preg_match().

 

Try googleing regular expressions and then look up those functions in the manual.

 

Good Luck.

Link to comment
Share on other sites

Thanks for the reply.

 

I was hoping I find a simple built-in function.

 

Anyway , How do you store a string that is bbetween to tags into a variable ? how do you select it?

how do you select "php content" from    <p>php content</p>    ?

Link to comment
Share on other sites

If this is the case, you may want to look into templating engines or a template class that would provide these functions.  However these can be quite bulky and slow your script down.

 

If you are feeling adventurous you can code your own.  To start you off, you may want something like this :

function ReplacePelems($tcontent, $static_data) {
    foreach ($static_data as $prefix => $new_data) {

    		$rowbegin = "<p name='" . $prefix . "'>";
        		$row_b_len = strlen($rowbegin);
        		$startrow = strpos($tcontent, $rowbegin);

        		$rowend = "</p name='" . $prefix . "'>";
        		$row_e_len = strlen($rowend);
        		$endrow = strpos($tcontent, $rowend) + $row_e_len;

        		$rowlen = $endrow - $startrow;
        		$row = substr($tcontent, $startrow, $rowlen);

        		$tcontent = str_replace($row, $new_data, $tcontent);
        		unset($rows);
    }

    return $tcontent;
}

 

I just edited a function I had lying around and havn't tested it for u, but should put u in the right direction.  Have a play around.  If you are feeling adventurous you can replace the p with a preg_replace clause for elements (you should be able to find on the net) and change the $rowend with the result.

 

I put the name="" bit in , as I didn't know how complex your pages are.  $tcontent should be a getcontent result of the file you want to cahnge, and $static_data should be an array of the bits you want to change. keys should be what is specified in name="" and values should be what you want in between the elements.

 

Good luck

Link to comment
Share on other sites

unfortunately I don't know how exactly the HTML data is, Thanks for taking time, but that function won't work the way i want.

 

there maybe various of tags, various of attributes, I jus twant to tell it "search the data which is not in the tags", and make them to link.

 

???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.