mazman13 Posted September 11, 2006 Share Posted September 11, 2006 I'm making a blog for someone who has no, HTML knowlegde. So I want to make it easy.It's something very simple, just a title, body, and it holds the date and time in the Database.Everything is working great...I just need 2 things...I want it so that, when they press "enter" it auto. adds the breaks and spaces. I already know about nl2br(); but it's not doing anything. Is it possible that the textbox is not sending /n?? What can I do for PHP to know that there is an end?Also, is there a pretty simple way to do this? "and check out my website at www.michaelzavala.com..."And have PHP fine that www. and .com and add HTML to it auto?I hope you guys understand! I hope yall can help! Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 11, 2006 Share Posted September 11, 2006 [quote author=mazman13 link=topic=107687.msg432301#msg432301 date=1158007592]Also, is there a pretty simple way to do this? "and check out my website at www.michaelzavala.com..."And have PHP fine that www. and .com and add HTML to it auto?[/quote]Not really because it's not possible to guess that some words need www. and .com added, while other words don't. The best way to generate links from URLs is as posted in the thread here -> http://www.phpfreaks.com/forums/index.php/topic,106159.0.html Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 11, 2006 Author Share Posted September 11, 2006 Thanks Andy.I'll prob, just scratch that idea for now. :) But the enter thing is something I need for sure. If anyone knows how to do this, that would be great. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 11, 2006 Share Posted September 11, 2006 I'm not too sure how you've implemented the enter stuff in a text box, pass it to a script, enter it in a database and subsequently retrieve it from a database and output it to screen, but ... if nl2br($retrieved_text) doesn't contain the line breaks you entered in the text box it must be because you stripped them out before putting the data into the database. Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 12, 2006 Author Share Posted September 12, 2006 Here is what happens after someone types in the text field and clicks submit:if($action == "add"){//If ADD, send info to Datebase//Grabs date and time$dub_date = date("Y-m-d H:i:s");//Converts to Uppercase words in the TITLE$rewrite = ucwords("$dub_title");$dub_title = $rewrite;//Removes PHP and HTML tags in the TITLE$rewrite = strip_tags($dub_title);$dub_title = $rewrite;[color=orange]//Checks infostr_replace(array("\r\n", "\n", "\r"), "", $dub_body);echo($dub_body);[/color][b]THIS DOES NOT WORK, I've tried using nl2br() and nothing happens. I don't know if the textbox sends the \n when we submit it.[/b]//Adds info to Datebase $query = "INSERT INTO $database (dub_title,dub_body,dub_date) VALUES('$dub_title','$dub_body','$dub_date')"; $result = mysql_query($query) or die ("Can't do anything with the query!"); echo "New blog added.<br>";}; Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 12, 2006 Share Posted September 12, 2006 [code]//Checks infostr_replace(array("\r\n", "\n", "\r"), "", $dub_body);echo($dub_body);THIS DOES NOT WORK, I've tried using nl2br() and nothing happens. I don't know if the textbox sends the \n when we submit it.[/code]Remove ALL that.When you [b]retrieve[/b] the data ... echo nl2br($row['dub_body']); // or equivalent Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 12, 2006 Author Share Posted September 12, 2006 Thanks again AndyI did this:$row = mysql_fetch_array($result,MYSQL_ASSOC); extract($row); [b] nl2br('$dub_body');[/b]echo "<b>$dub_title</b> - <a href=\"$_SERVER[PHP_SELF]?action=delete_post&dub_id=$dub_id&name=$dub_title\">[Delete]</a> <a href=\"$_SERVER[PHP_SELF]?action=edit_post&dub_id=$dub_id&dub_title=$dub_title&dub_body=$dub_body\">[Edit]</a><br />";echo "$dub_body<br><font size=\"1\"><i>$dub_date</i></font><br /><br />";My input was:this is a linei hope this breaksMy output was:this is a line i hope this breaksI've been having this problem all weekend. Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 15, 2006 Author Share Posted September 15, 2006 Alright, I'm a little confused, I'm still a tab bit new with php.There is no simple way to auto-format a string, I got that.So what do I do if my string it:"I saw a website and it was www.cool.com"I want www.cool.com to be a link...can I add something before and after the www.cool.com and have a function, search and replace it with html? That prob won't work.But I just need some ideas, and maybe some explainin' to get my mind wrapped around it!Thanks-Michael Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 [quote author=mazman13 link=topic=107687.msg434967#msg434967 date=1158336482]So what do I do if my string it:"I saw a website and it was www.cool.com"I want www.cool.com to be a link...[/quote]Yup. Look at the link I posted earlier in this thread that will automatically convert www.cool.com into a clickable hyperlink on output. Trust me, it works. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 [quote author=mazman13 link=topic=107687.msg432450#msg432450 date=1158023006]Thanks again AndyI did this .... [/quote]So change it to:[code]$row = mysql_fetch_array($result,MYSQL_ASSOC); extract($row);echo "<b>$dub_title</b> - <a href=\"$_SERVER[PHP_SELF]?action=delete_post&dub_id=$dub_id&name=$dub_title\">[Delete]</a> <a href=\"$_SERVER[PHP_SELF]?action=edit_post&dub_id=$dub_id&dub_title=$dub_title&dub_body=$dub_body\">[Edit]</a><br />";echo nl2br($dub_body). "<br><font size=\"1\"><i>$dub_date</i></font><br /><br />";...[/code] Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 15, 2006 Author Share Posted September 15, 2006 Ok! I def wanna use it, I'm just having a hard time following it. Would I have to customize it or anything? Do I erase the $test array? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 [code] function clickable_short_url ($matches) { ### Entire match. $url = array_shift($matches); ### Only http:// match, if any. $http = array_shift($matches); ### Shorten textual part if need be. $url_text = strlen($url) >= 30 ? (substr($url, 0, 20) . '...' . substr($url, -10, 10)) : $url ; ### Add http:// prefix if need be. if (! $http) { $url = 'http://' . $url; } ### Return new url. return '<a target="_blank" href="' . $url . '">' . $url_text . '</a>'; } $pattern = '% (http://)? ### optional http:// prefix (?(1)|www\.) ### require www. if there is no http:// \S+ ### gobble anything but white space %x';// conn to database, select row, etc. and suppose the output you want is $dub_body ....// just pass the text you want converted to the function ...echo nl2br(clickable_short_url($dub_body));[/code] Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 15, 2006 Author Share Posted September 15, 2006 Ok, that works, but i'm having array_shift() errors, I guess since there is no array its freakin' out. What do I replace that with? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 Heh, that'll teach me to post without thinking. Sorry.Here's working code as I have it on one of my own sites. The function and the pattern definition as I posted here and then to output the database thing with clickable links...[code]echo nl2br(preg_replace_callback($pattern, 'clickable_short_url', $row['dub_body']));[/code] Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 15, 2006 Author Share Posted September 15, 2006 So I still use the same function you posted? And then use the new code you posted for the output? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 Yes (the function AND the pattern definition) and Yes (the one-liner) Quote Link to comment Share on other sites More sharing options...
mazman13 Posted September 15, 2006 Author Share Posted September 15, 2006 Thanks Andy-I got it now. The lightbulb went on.I did have a probem, on this blog, the links will show up, but if your adding and html embed for a youtube video...it takes that out. lol So i think i'm gonna stick with one or the other.Thanks for your help!Does the script you posted all deal with .net, .org, and so on? Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2006 Share Posted September 15, 2006 [quote author=mazman13 link=topic=107687.msg435161#msg435161 date=1158354905]Does the script you posted all deal with .net, .org, and so on?[/quote]Yes, but don't take my word for it ... give it a try :) 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.