Jump to content


Photo

auto Format Links in TEXT AREA for blogs


  • Please log in to reply
17 replies to this topic

#1 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 11 September 2006 - 08:46 PM

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!

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 September 2006 - 09:29 PM

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?


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...c,106159.0.html
Legend has it that reading the manual never killed anyone.
My site

#3 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 11 September 2006 - 11:03 PM

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.

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 September 2006 - 11:21 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#5 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 12 September 2006 - 12:53 AM

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;

//Checks info
str_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.

//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>";
};

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 12 September 2006 - 12:57 AM

//Checks info
str_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.

Remove ALL that.

When you retrieve the data ... echo nl2br($row['dub_body']); // or equivalent
Legend has it that reading the manual never killed anyone.
My site

#7 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 12 September 2006 - 01:03 AM

Thanks again Andy

I did this:

$row = mysql_fetch_array($result,MYSQL_ASSOC);
extract($row);
nl2br('$dub_body');

echo "<b>$dub_title</b> - <a href=\"$_SERVER[PHP_SELF]?action=delete_post&dub_id=$dub_id&name=$dub_title\">[Delete]</a>&nbsp;&nbsp;<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 line
i hope this breaks

My output was:
this is a line i hope this breaks

I've been having this problem all weekend.



#8 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 15 September 2006 - 04:08 PM

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

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 04:10 PM

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...


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.
Legend has it that reading the manual never killed anyone.
My site

#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 04:13 PM

Thanks again Andy

I did this ....


So change it to:

$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>&nbsp;&nbsp;<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 />";
...

Legend has it that reading the manual never killed anyone.
My site

#11 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 15 September 2006 - 04:15 PM

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?

#12 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 04:21 PM

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));

Legend has it that reading the manual never killed anyone.
My site

#13 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 15 September 2006 - 04:27 PM

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?

#14 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 04:46 PM

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...

echo nl2br(preg_replace_callback($pattern, 'clickable_short_url', $row['dub_body']));

Legend has it that reading the manual never killed anyone.
My site

#15 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 15 September 2006 - 04:50 PM

So I still use the same function you posted? And then use the new code you posted for the output?

#16 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 07:06 PM

Yes (the function AND the pattern definition) and Yes (the one-liner)
Legend has it that reading the manual never killed anyone.
My site

#17 mazman13

mazman13
  • Members
  • PipPipPip
  • Advanced Member
  • 156 posts

Posted 15 September 2006 - 09:15 PM

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?

#18 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 September 2006 - 09:19 PM

Does the script you posted all deal with .net, .org, and so on?


Yes, but don't take my word for it ... give it a try :)
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users