Jump to content

auto Format Links in TEXT AREA for blogs


mazman13

Recommended Posts

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!
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 info
str_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>";
};
Link to comment
Share on other sites

[code]//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.[/code]

Remove ALL that.

When you [b]retrieve[/b] the data ... echo nl2br($row['dub_body']); // or equivalent
Link to comment
Share on other sites

Thanks again Andy

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[quote author=mazman13 link=topic=107687.msg432450#msg432450 date=1158023006]
Thanks again Andy

I 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>&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 />";
...
[/code]
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.