anarchoi Posted November 10, 2006 Share Posted November 10, 2006 Ok i have this code that selects a list of words in a row of my db:$query = "SELECT title FROM zine_pages_categories";$result = mysql_query($query);while($row = mysql_fetch_assoc($result)) { echo $row["title"]."<br />";}what i'd like to do now, say i have a text called $text, i want to replace all values found in "TITLE" in my $text by a link (where the link name is the corresponding "title")i could use a preg_replace() to do that but problem is that there is more than 1 result in "title" and i'm not an experienced programmerany help? Quote Link to comment Share on other sites More sharing options...
btherl Posted November 10, 2006 Share Posted November 10, 2006 Can you give an example of what you want to do? Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 10, 2006 Author Share Posted November 10, 2006 Ok i have a text with a lot of words...And i have a table in my database containing "title" which is a lot of names...Say i have Peter Kropotkine and Joseph Proudhon in my databaseAnd say my text with a lot of words is $texti'd like to replace ALL words "Peter Kropotkine" and "Joseph Proudhon" in $text by something like:<a href=\"mypage.php?name=Peter%20Kropotkine\">Peter Kropotkine</a>I can do that with a preg_replace() but there is not ONLY Peter Kropotkine in my database, i got a few others 50 names to check. Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 10, 2006 Share Posted November 10, 2006 you could do something like this:ex[code=php:0]<?php$text = "Here´s your string with a lot of text anda few names like Peter Kropotkine and Joseph Proudhon";include("db_connection.php");$sql = "SELECT * FROM your_table";$result = mysql_query($sql);while($row = mysql_fetch_assoc($result)) { $name = $row['name']; // name from your database $text = preg_replace("/($name)/ise", '"<a href=\"mypage.php?name=" . urlencode("\\1") . "\">\\1</a>"', $text);}echo $text;?>[/code] Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 10, 2006 Author Share Posted November 10, 2006 hmmm unless i did something wrong it echo'es the same text like 50 time :/ Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 11, 2006 Author Share Posted November 11, 2006 here's my page:http://anarchoi.yi.org/zine/modules.php?name=Bibliotheque Quote Link to comment Share on other sites More sharing options...
Nicklas Posted November 11, 2006 Share Posted November 11, 2006 Show us some code Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 12, 2006 Author Share Posted November 12, 2006 well all i did was replacing db variables in your code by phpnuke's db variables$textx = "Here´s your string with a lot of text and a few names like Peter Kropotkine and Joseph Proudhon bla bla KROPOTKINE, Pierre bla bla";$result = $db->sql_query("SELECT * FROM ".$prefix."_pages_categories");while($row = mysql_fetch_assoc($result)) { $name = $row['title']; // name from your database $textx = preg_replace("/($name)/ise", '"<a href=\"mypage.php?name=" . urlencode("\\1") . "\">\\1</a>"', $textx); echo "$textx";} Quote Link to comment Share on other sites More sharing options...
Adika Posted November 12, 2006 Share Posted November 12, 2006 Hi anarchoi.Why do you write [/url] instead of </a>?$textx = preg_replace("/($name)/ise", '"<a href=\"mypage.php?name=" . urlencode("\\1") . "\">\\1[color=red][/url][/color]"', $textx); Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 12, 2006 Author Share Posted November 12, 2006 i think thats the forum scripts that auto-convert html tags into a phpBBtag-like code cuz i haven't touched Nicklas's code except to make the DB connection... and there's no [/url] in the code Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 13, 2006 Share Posted November 13, 2006 Try this:[code]<?php$text = 'Here is your string with a lot of textand a few names like Peter Kropotkine and Joseph Proudhon';$names = array();$names[] = 'Joseph Proudhon';$names[] = 'Peter Kropotkine';foreach($names as $name){ $text = str_replace($name,'<a href="mypage.php?name=' . str_replace(' ', '%20',$name) . '">' . $name . '<' . '/a' . '>', $text);}echo $text;?>[/code]BTW--you can combine this part into ONE string. I seperated it out so that the board wouldn't convert it to bbCode.[b]'<' . '/a' . '>'[/b]This is the HTML code that's sent to my browser when I ran this.[quote]Here is your string with a lot of textand a few names like <a href="mypage.php?name=Peter%20Kropotkine">Peter Kropotkine</a> and <a href="mypage.php?name=Joseph%20Proudhon">Joseph Proudhon</a>[/quote] Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 13, 2006 Author Share Posted November 13, 2006 hmmm any way you can make it look for field 'title' in table 'zine_pages_categories' to automaticaly add all results found 'title' in $names[]$names = array();$names[] = 'Joseph Proudhon';$names[] = 'Peter Kropotkine';i'd need something that would replace 'Joseph Proudhon' and 'Peter Kropotkine' by each entries in 'title' Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 14, 2006 Author Share Posted November 14, 2006 any help :/this is my last question, i promise :) Quote Link to comment Share on other sites More sharing options...
anarchoi Posted November 17, 2006 Author Share Posted November 17, 2006 bump Quote Link to comment Share on other sites More sharing options...
Adika Posted November 17, 2006 Share Posted November 17, 2006 Sure.[code]$names = array();$result = $db->sql_query("SELECT * FROM ".$prefix."_pages_categories");while($row = mysql_fetch_assoc($result)) {// creates an array $names, where the names from $row['title'] are stored $names[] = $row['title'];}// replace each name with linkforeach($names as $name){ $text = str_replace($name,'<a href="mypage.php?name=' . str_replace(' ', '%20',$name) . '">' . $name . '<' . '/a' . '>', $text);}echo $text;[/code] Quote Link to comment 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.