3 Replies - 1419 Views - Last Post: 17 May 2007 - 10:07 PM Rate Topic: -----

#1 kagitpas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 07

wraparound text in PHP

Posted 17 May 2007 - 12:43 PM

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

Is This A Good Question/Topic? 0
  • +

Replies To: wraparound text in PHP

#2 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: wraparound text in PHP

Posted 17 May 2007 - 03:26 PM

Check out how to use chunk_split and nl2br (or str_split (php5) and implode)
Was This Post Helpful? 0
  • +
  • -

#3 kagitpas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 07

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!
Was This Post Helpful? 0
  • +
  • -

#4 Styx  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 192
  • Joined: 04-March 07

Re: wraparound text in PHP

Posted 17 May 2007 - 10:07 PM

that works ^_^ you could use nl2br with that too if you needed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1