FoxRocks Posted September 25, 2012 Share Posted September 25, 2012 Hello, First, I'm sorry my title probably doesn't make sense, I couldn't think of a better way to put it. Basically I came up with an idea in my head that I now want to make happen. Here it is. Let's say I enter some data into a database table using a regular html form, for example like the text below: Here is some text, for more information, Contact Us! What I want to do is somehow (magic?) make it so when I retrieve this same text out of the database, it makes Contact Us into a link to the contact page. Crazy, right? I know, I just thought it would be really cool if when my customer adds another item to their websites FAQ page, that any time they say "Contact Us" it would display as a link to their contact page. Thoughts? ~FOX~ Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/ Share on other sites More sharing options...
Lyleyboy Posted September 25, 2012 Share Posted September 25, 2012 I'd say the best bet would be to write a function to replace the string "Contact Us" with the link to that page. Regex should do that for you. If you're using a CMS you could list out the pages and find the page name and replace that with the <a href="contactus.php">Contact Us</a> structure. Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1380780 Share on other sites More sharing options...
Jessica Posted September 25, 2012 Share Posted September 25, 2012 Regex might even be overkill, Plain string functions can do this. Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1380783 Share on other sites More sharing options...
White_Lily Posted September 25, 2012 Share Posted September 25, 2012 (edited) Try this: <?php $query = mysql_query("SELECT * FROM table_name"); if(mysql_num_rows($query) != 0){ $row = mysql_fethc_assoc($query); $contactUs = $row["pageContent"]; $convertContact = str_replace("Contact Us", "<a href='contactus.php'>Contact Us</a>", $contactUs); $convertContact = $contactUs; echo $contactUs; }else{ echo "Nothing in database"; } ?> Edited September 25, 2012 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1380787 Share on other sites More sharing options...
Christian F. Posted September 25, 2012 Share Posted September 25, 2012 (edited) I'm afraid that won't work, White_Lily, as you're overwriting the corrected string with the uncorrected. Use this instead: $query = mysql_query("SELECT * FROM table_name"); if(mysql_num_rows($query) != 0){ $row = mysql_fethc_assoc($query); $contactUs = str_replace("Contact Us", "<a href='contactus.php'>Contact Us</a>", $row["pageContent"]); echo $contactUs; }else{ echo "Nothing in database"; } Don't really need the $contactUs variable either, as you could use the pageContent index directly. That is, unless you need the unmodified string later on. Edited September 25, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1380806 Share on other sites More sharing options...
White_Lily Posted September 25, 2012 Share Posted September 25, 2012 Ah my mistake ive always used my method when doing this sort of thing lol. PS: Mispelled "fetch" in both Cristian's and my on post(s). Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1380810 Share on other sites More sharing options...
dodgeitorelse3 Posted September 26, 2012 Share Posted September 26, 2012 why not just put link into database? Is there a downside to that? <a href="contactus.php">Contact Us</a> Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1381008 Share on other sites More sharing options...
FoxRocks Posted September 30, 2012 Author Share Posted September 30, 2012 Hey! Thank you all so much, I appreciate you taking the time to reply! I re-read my question and I don't think I explained myself very well, I remember being sort of fragmented at the time I wrote it. I think the str_replace function might be the ticket (magic) I was looking for, but I want to try to re-write the question again so I can make sure I'm being clear with what I'm trying to do. So... Let's say I've got a web page for Frequently Asked Questions that I want to allow my customer to add new FAQ's to. With that in mind, I've made a CMS form that inserts new data into the database. The FAQ page then queries that database and prints the information out accordingly. So far so good, easy peasy. So let's say my customer, through the CMS form, inserts this text into the database: "Yes, we can do that, please Contact Us for a quote." What I want to do is make it so when the FAQ page queries the database, and prints out the text above, it replaces the words "Contact Us" with <a href="/contact-us/">Contact Us</a>. So it creates a link to the contact page without my customer having to enter any html into the CMS form. There, I think that sounds a lot better. However I do believe the str_replace function will be the answer I was looking for, so I'm going to go and give that a try. Thanks again for the help ~FOX~ Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1381874 Share on other sites More sharing options...
FoxRocks Posted October 1, 2012 Author Share Posted October 1, 2012 (edited) Oh, how very cool! The str_replace function did exactly what I wanted it to, thank you so much for helping me get pointed into the right direction. Here is what I did: $contact_link = "<a href=\"/contact-us/\" class=\"inline\" target=\"_parent\">Contact Us</a>"; $answer = str_replace(array("contact us","Contact Us","Contact us") , array($contact_link , $contact_link , $contact_link), $row['answer']); echo "<td>" . $answer . "</td>"; ...and it works just exactly as I had imagined it, I am so happy I would be interested to know if there is anything I missed, or if there is a cleaner way to run this...maybe in a function? Hmmm...I just thought of the function idea as I typed this, maybe I'll go work on that one next. Thanks again everyone, I appreciate the help! ~FOX~ (not going to mark solved until I make sure it's complete) Edited October 1, 2012 by FoxRocks Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1382055 Share on other sites More sharing options...
White_Lily Posted October 1, 2012 Share Posted October 1, 2012 You could this in a function, however functions are only really useful if this same thing is used throughout the entire site. Other than that I don't think it can be any cleaner Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1382057 Share on other sites More sharing options...
FoxRocks Posted October 1, 2012 Author Share Posted October 1, 2012 Thanks White_Lily, It's nice to hear I didn't make a mess of it Also, I did try to make it into a function, but the function would have been bigger than just writing the code, so I decided to skip it. Thanks again, ~FOX~ Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1382061 Share on other sites More sharing options...
Christian F. Posted October 1, 2012 Share Posted October 1, 2012 Could have been a bit cleaner, by using str_ireplace (). Other than that this looks golden. Quote Link to comment https://forums.phpfreaks.com/topic/268776-how-do-i-create-a-link-from-part-of-the-text-in-a-database/#findComment-1382069 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.