asmith Posted August 19, 2008 Share Posted August 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/120339-change-words-to-links-in-an-html-content/ Share on other sites More sharing options...
scottybwoy Posted August 19, 2008 Share Posted August 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120339-change-words-to-links-in-an-html-content/#findComment-620014 Share on other sites More sharing options...
asmith Posted August 19, 2008 Author Share Posted August 19, 2008 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> ? Quote Link to comment https://forums.phpfreaks.com/topic/120339-change-words-to-links-in-an-html-content/#findComment-620023 Share on other sites More sharing options...
scottybwoy Posted August 19, 2008 Share Posted August 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/120339-change-words-to-links-in-an-html-content/#findComment-620049 Share on other sites More sharing options...
asmith Posted August 19, 2008 Author Share Posted August 19, 2008 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. ??? Quote Link to comment https://forums.phpfreaks.com/topic/120339-change-words-to-links-in-an-html-content/#findComment-620286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.