I am new to PHP and need help in having the output shortened URL wraparound into a smaller width. Right now, the text goes into one continuous line and makes the resultant page really wide if the number of characters in the long URL is very long.
You can test it here at : http://www.jrurl.com
Here is the create page PHP:
<?php
ob_start();
require("config.php");
require("global.php");
require("header.php");
?>
<div class="MainTitle"></div>
<td width="588"><div class="Body"><em></em><br />
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<?php
$error = false;
$url = trim(urldecode($_POST['url']));
if ($config['captcha']) {
session_start();
if (!$_SESSION['captcha_phrase'] || $_SESSION['captcha_phrase'] != $_POST['captcha']) {
$error = true;
echo "<div class=\"ErrorBody\">You have entered wrong security code, please try again.</div><br />\n";
}
}
if (!empty($_POST['url']) && !$error) {
if ($url{strlen($url) - 1} == "/") {
$url = substr($url, 0, -1);
}
if (!preg_match("/^(ht|f)t(p|ps)\:\/\//si", $url)) {
$url = "http://".$url;
}
$length = strlen($url);
$code = 0;
$count = 0;
db_connect();
do {
$short_url = strtolower(substr(md5((($code > 0) ? $code : "").$url), 0, 6));
$suffix = $short_url{0};
$result = mysql_query("SELECT url FROM url_{$suffix} WHERE short_url = '$short_url'") or die(mysql_error());
$count = mysql_num_rows($result);
if ($count > 0) {
$row = mysql_fetch_row($result);
if (stripslashes($row[0]) == $url) {
break;
}
$code++;
}
else {
mysql_query("INSERT INTO url_{$suffix} (short_url, url, created) VALUES ('$short_url', '".addslashes($url)."', NOW())") or die(mysql_error());
break;
}
} while ($count > 0);
$short_url = "http://".$config['domain']."/".((!$config['rewrite']) ? "?" : "").$short_url;
$short_length = strlen($short_url);
echo "<div class=\"ResultTitle\"><b>URL has been shortened!</b></div><br />\n".
"<div class=\"ResultBody\">The following URL:<br /><br />\n".
"<b>$url</b><br /><br />\n".
"has a length of $length characters and resulted in the following shortened URL which has a length of $short_length characters. Make sure you copy it down!<br /><br />\n".
"<b>$short_url</b><br /><br />\n".
"[ <a href=\"$short_url\" target=\"_blank\">Open in new window</a> ]</div>\n";
}
else {
echo "<div class=\"ErrorBody\"><b>A URL was not entered, please try again.</b></div><br />\n";
}
if ($config['captcha']) {
mt_srand((double) microtime() * 1000000);
$_SESSION['captcha_phrase'] = substr(strtoupper(md5(mt_rand())), 0, 6);
}
?>
</td>
</tr>
</table>
<form method="post" action="create.php">
<div class="FormBody">
<b>Enter a long URL to make it shorter:</b><br />
<input type="text" name="url" size="40" /><br />
<?php
if ($config['captcha']) {
echo "<b>Security code:</b><br />\n".
"<img src=\"captcha.php\" /> <input type=\"text\" name=\"captcha\" size=\"10\" /><br />\n";
}
?>
<input type="submit" value="Make Shorter" />
</div>
</form>
<?php
require("footer.php");
ob_end_flush();
?>
wraparound text in PHP
Page 1 of 13 Replies - 1252 Views - Last Post: 17 May 2007 - 10:07 PM
Replies To: wraparound text in PHP
#2
Re: wraparound text in PHP
Posted 17 May 2007 - 03:26 PM
#3
Re: wraparound text in PHP
Posted 17 May 2007 - 03:37 PM
Thank you. In the meantime (before the post) I found out how to use wordwrap and was able to insert the following line:
$wrap_url = wordwrap($url, 75, "\n", true);
this worked like a charm!
$wrap_url = wordwrap($url, 75, "\n", true);
this worked like a charm!
#4
Re: wraparound text in PHP
Posted 17 May 2007 - 10:07 PM
that works
you could use nl2br with that too if you needed
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|