Jump to content

Create a function


Matpatnik
 Share

Recommended Posts

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

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

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...