Matpatnik Posted January 22, 2008 Share Posted January 22, 2008 Hi guys, I've created a function that look at a specific word in any king of string and replace it by a link. I wonder if there is a better way of doing it. function link_Elements($Text,$lang) { ###----- START selecting elements table -----### $db = new MySqlDB; $db->connect('localhost', '...', '...'); $db->select_db('...'); $result = $db->query("SELECT id, abr, name_". $lang ." FROM ..."); while ($row = $db->fetch_array($result)) { $elem_link = '<a href="'. $_SERVER['PHP_SELF'] .'?section=encyclopedia&cat=5&element='. $row['id'] .'" title="'. $row['name_'. $lang] .' ('. $row['abr'] .')">'; $Text = preg_replace('/( '. $row['name_'. $lang] .'s )|( '. strtolower($row['name_'. $lang]) .'s )/', ' '. $elem_link . $row['name_'. $lang] .'s</a> ', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'s,)|( '. strtolower($row['name_'. $lang]) .'s,)/', ' '. $elem_link . $row['name_'. $lang] .'s</a>,', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'s\.)|( '. strtolower($row['name_'. $lang]) .'s\.)/', ' '. $elem_link . $row['name_'. $lang] .'s</a>.', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'s:)|( '. strtolower($row['name_'. $lang]) .'s:)/', ' '. $elem_link . $row['name_'. $lang] .'s</a>:', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'s;)|( '. strtolower($row['name_'. $lang]) .'s;)/', ' '. $elem_link . $row['name_'. $lang] .'s</a>;', $Text); $Text = preg_replace('/(\"'. $row['name_'. $lang] .'s\")|(\"'. strtolower($row['name_'. $lang]) .'s\")/', '"'. $elem_link . $row['name_'. $lang] .'s</a>"', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'s\))|( '. strtolower($row['name_'. $lang]) .'s\))/', ' '. $elem_link . $row['name_'. $lang] .'s</a>)', $Text); $Text = preg_replace('/(\('. $row['name_'. $lang] .'s\))|(\('. strtolower($row['name_'. $lang]) .'s\))/', '('. $elem_link . $row['name_'. $lang] .'s</a>)', $Text); $Text = preg_replace('/(\('. $row['name_'. $lang] .'s )|(\('. strtolower($row['name_'. $lang]) .'s )/', '('. $elem_link . $row['name_'. $lang] .'s</a> ', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .' )|( '. strtolower($row['name_'. $lang]) .' )/', ' '. $elem_link . $row['name_'. $lang] .'</a> ', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .',)|( '. strtolower($row['name_'. $lang]) .',)/', ' '. $elem_link . $row['name_'. $lang] .'</a>,', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'\.)|( '. strtolower($row['name_'. $lang]) .'\.)/', ' '. $elem_link . $row['name_'. $lang] .'</a>.', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .':)|( '. strtolower($row['name_'. $lang]) .':)/', ' '. $elem_link . $row['name_'. $lang] .'</a>:', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .';)|( '. strtolower($row['name_'. $lang]) .';)/', ' '. $elem_link . $row['name_'. $lang] .'</a>;', $Text); $Text = preg_replace('/(\"'. $row['name_'. $lang] .'\")|(\"'. strtolower($row['name_'. $lang]) .'\")/', '"'. $elem_link . $row['name_'. $lang] .'</a>"', $Text); $Text = preg_replace('/( '. $row['name_'. $lang] .'\))|( '. strtolower($row['name_'. $lang]) .'\))/', ' '. $elem_link . $row['name_'. $lang] .'</a>)', $Text); $Text = preg_replace('/(\('. $row['name_'. $lang] .'\))|(\('. strtolower($row['name_'. $lang]) .'\))/', '('. $elem_link . $row['name_'. $lang] .'</a>)', $Text); $Text = preg_replace('/(\('. $row['name_'. $lang] .')|(\('. strtolower($row['name_'. $lang]) .')/', '('. $elem_link . $row['name_'. $lang] .'</a> ', $Text); $Text = preg_replace('/\('. $row['abr'] .'\)/', '('. $elem_link . $row['abr'] .'</a>)', $Text); } $db = $db->close(); //------------- END selecting elements table --------------> return $Text;} Thank you for your help. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 23, 2008 Share Posted January 23, 2008 You can use str_replace with arrays, you can have one array of words to search for and another array of words to replace with.http://www.php.net/manual/en/function.str-replace.php Link to comment Share on other sites More sharing options...
Matpatnik Posted January 23, 2008 Author Share Posted January 23, 2008 Can you give me an example please because I don't see how I can use the array with it.I did this so fare: function link_Elements($Text,$lang) { ###----- START selecting elements table -----### $db = new MySqlDB; $db->connect('localhost', '...', '...'); $db->select_db('...'); $result = $db->query("SELECT id, abr, name_". $lang ." FROM ..."); while ($row = $db->fetch_array($result)) { $elem_link = '<a href="'. $_SERVER['PHP_SELF'] .'?section=encyclopedia&cat=5&element='. $row['id'] .'" title="'. $row['name_'. $lang] .' ['. $row['abr'] .']">'; $Text = str_ireplace(' '. $row['name_'. $lang] .'s ', ' '. $elem_link . $row['name_'. $lang] .'s</a> ', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .'s,', ' '. $elem_link . $row['name_'. $lang] .'s</a>,', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .'s.', ' '. $elem_link . $row['name_'. $lang] .'s</a>.', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .'s:', ' '. $elem_link . $row['name_'. $lang] .'s</a>:', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .'s;', ' '. $elem_link . $row['name_'. $lang] .'s</a>;', $Text); $Text = str_ireplace('"'. $row['name_'. $lang] .'s"', '"'. $elem_link . $row['name_'. $lang] .'s</a>"', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .'s)', ' '. $elem_link . $row['name_'. $lang] .'s</a>)', $Text); $Text = str_ireplace('('. $row['name_'. $lang] .'s)', '('. $elem_link . $row['name_'. $lang] .'s</a>)', $Text); $Text = str_ireplace('('. $row['name_'. $lang] .'s ', '('. $elem_link . $row['name_'. $lang] .'s</a> ', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .' ', ' '. $elem_link . $row['name_'. $lang] .'</a> ', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .',', ' '. $elem_link . $row['name_'. $lang] .'</a>,', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .'.', ' '. $elem_link . $row['name_'. $lang] .'</a>.', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .':', ' '. $elem_link . $row['name_'. $lang] .'</a>:', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .';', ' '. $elem_link . $row['name_'. $lang] .'</a>;', $Text); $Text = str_ireplace('"'. $row['name_'. $lang] .'"', '"'. $elem_link . $row['name_'. $lang] .'</a>"', $Text); $Text = str_ireplace(' '. $row['name_'. $lang] .')', ' '. $elem_link . $row['name_'. $lang] .'</a>)', $Text); $Text = str_ireplace('('. $row['name_'. $lang] .')', '('. $elem_link . $row['name_'. $lang] .'</a>)', $Text); $Text = str_ireplace('('. $row['name_'. $lang] .' ', '('. $elem_link . $row['name_'. $lang] .'</a> ', $Text); $Text = str_ireplace('>'. $row['name_'. $lang] .' ', '>'. $elem_link . $row['name_'. $lang] .'</a> ', $Text); $Text = str_ireplace('('. $row['abr'] .')', '('. $elem_link . $row['abr'] .'</a>)', $Text); $Text = str_ireplace('('. $row['abr'] .',', '('. $elem_link . $row['abr'] .'</a>,', $Text); $Text = str_ireplace(', '. $row['abr'] .',', ', '. $elem_link . $row['abr'] .'</a>,', $Text); $Text = str_ireplace(' '. $row['abr'] .')', ' '. $elem_link . $row['abr'] .'</a>)', $Text); } $db = $db->close(); //------------- END selecting elements table --------------> return $Text;} Link to comment Share on other sites More sharing options...
justsomeguy Posted January 24, 2008 Share Posted January 24, 2008 You're replacing a bunch of substrings in the same variable. For example, you're replacing this:' '. $row['name_'. $lang] .'s 'with this:' '. $elem_link . $row['name_'. $lang] .'s</a> 'and this:' '. $row['name_'. $lang] .'s,'with this:' '. $elem_link . $row['name_'. $lang] .'s</a>,'Everything is getting looked for inside the variable $Text. So you can put everything to search for in one array, and everything to replace with in another array, and then call the function once to find and replace everything. Check the examples in the manual for str_replace to see how to do it. Link to comment Share on other sites More sharing options...
Matpatnik Posted January 25, 2008 Author Share Posted January 25, 2008 Are you referring to this example? // Provides: You should eat pizza, beer, and ice cream every day$phrase = "You should eat fruits, vegetables, and fiber every day.";$healthy = array("fruits", "vegetables", "fiber");$yummy = array("pizza", "beer", "ice cream");$newphrase = str_replace($healthy, $yummy, $phrase);I thought this way or the way I did it was pretty much the same except in a different way.because if I use this example I still have to put every thing in two arrays which will make one of them very big.I just think about it and, yes, it's better make the server running then the db. I didn't think about it.I'll do it this way if that's what you meant. Link to comment Share on other sites More sharing options...
killboy Posted January 27, 2008 Share Posted January 27, 2008 I don't get it:What are all those $Text = str_ireplace(' '. $row['name_'. $lang] .')', ' '. $elem_link . $row['name_'. $lang] .'</a>)', $Text);for?? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2008 Share Posted January 28, 2008 Yeah, you can either have a ton of calls to str_ireplace or two arrays in memory. It's better to use the arrays and call str_ireplace once. Link to comment Share on other sites More sharing options...
Matpatnik Posted January 31, 2008 Author Share Posted January 31, 2008 I don't get it:What are all those $Text = str_ireplace(' '. $row['name_'. $lang] .')', ' '. $elem_link . $row['name_'. $lang] .'</a>)', $Text);for?? http://ca.php.net/manual/en/function.str-ireplace.php Link to comment Share on other sites More sharing options...
killboy Posted January 31, 2008 Share Posted January 31, 2008 Yeah, I know what the str_ireplace() function does; what I don't understand is why you're making so many calls to it Link to comment Share on other sites More sharing options...
Matpatnik Posted February 2, 2008 Author Share Posted February 2, 2008 Like justsomeguy told me I am gonna use two array instead and call the function once Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.