Jump to content

[SOLVED] Having strlen problems, any thoughts???


nathanhornby

Recommended Posts

Ok here's my code:

 

<?

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM links ORDER BY linkid DESC";

$result=mysql_query($query);

 

$num=mysql_numrows($result);

 

$i=0;

while ($i < $num)

{

 

$linkurl=mysql_result($result,$i,"linkurl");

$linkurl='<a href="'.$linkurl.'">'.$linkurl.'</a>';

 

if (strlen($linkurl) >= 70)

{

$linkurl = substr($linkurl, 0, 70).'...';

}

echo '

<span class="maintext">

<br>- '.$linkurl.'<br>

</span>

';

$i++;

}

?>

 

The data is being called fine, and the link is displaying (as a link) and contains the full string so it can be selected, however when it comes to actuall shotening the length of the displayed link (with strlen) it seems to be doing some odd things; the read out looks a little like this:

 

-

 

- http://youtube.com/wa...

 

- http://ww...

 

- http://www.consolisearch.com

 

As you can see it's definately not shortening by the 70 character limit I set, it's not even affecting the correct data (as the 2nd and 3rd url are both less than 70 characters they shouldnt even be shortened) and the first link doesn't display at all.

 

Am I missing something really obvious, or could it be something in the database itself, perhaps I need to set the field up specificly for dealing with the characters you see in urls (/?:. etc) ?

 

Any help appreciated,

 

Thanks.

Link to comment
Share on other sites

<span class="maintext">

'.$linkurl.'<br>
'.strlen($linkurl).'<br>

</span>

 

Ok the read out looks like this:

 

http://youtube.com/wa...

73

http://ww...

73

http://www.consolisearch.com

73

 

 

It's missing off the first URL completely, this is the only one that is actually longer than 70 characters.  None of those urls are 73 characters either :\.

 

 

Link to comment
Share on other sites

Nicely spotted; I changed it but it hasn't made any difference, so i'm guessing that syntax was still valid.

 

btw the links in the post are cut off where the text is cut off, but in reality those links are actually intact, it's only the text itself that's cut off as required. i.e. it actually reads:

 

<a href="http://youtube.com/watch?v=vAkOti7pJJg">http://youtube.com/wa...<br>
73<br>

</span>

<span class="maintext">

<a href="http://www.fallingpixel.com/news/article.php?id=62">http://ww...<br>
73<br>

</span>

<span class="maintext">

<a href="http://www.consolisearch.com">http://www.consolisearch.com</a...<br>
73<br>

 

 

Link to comment
Share on other sites

Ok I've changed it to the following (let me know if I misunderstood):

 

<?

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM links ORDER BY linkid DESC";
$result=mysql_query($query);

$num=mysql_num_rows($result);

for ($i=0;$i<$num;$i++) {


$linkurl=mysql_result($result,$i,"linkurl");
$url='<a href="'.$linkurl.'">'.$linkurl.'</a>';

if (strlen($url) >= 70)
{
   $url = substr($url, 0, 70).'...';
}
echo '
<span class="maintext">

'.$url.'<br>
'.strlen($linkurl).'<br>

</span>
';

}
?>

 

The read out now looks like this:

 

http://youtube.com/wa...

38

http://ww...

50

http://www.consolisearch.com

28

 

So at least now it knows how many characters are there ... but it doesnt seem to be shortening correctly; and the first link still isn't displaying.

Link to comment
Share on other sites

Ah I think I might have it,

 

Changed

 

$linkurl=mysql_result($result,$i,"linkurl");
$url='<a href="'.$linkurl.'">'.$linkurl.'</a>';

if (strlen($url) >= 70)
{
   $linkurl = substr($linkurl, 0, 70).'...';
}echo '
<span class="maintext">

'.$url.'<br>
'.strlen($linkurl).'<br>

</span>

 

to

 

$linkurl=mysql_result($result,$i,"linkurl");
$url='<a href="'.$linkurl.'">'.$linkurl.'</a>';

if (strlen($url) >= 70)
{
   $url = substr($linkurl, 0, 70).'...';
}echo '
<span class="maintext">

'.$url.'<br>
'.strlen($linkurl).'<br>

</span>

 

Im guessing that was what you meant ... although now the '...' is coming up unconditionally, whether it has 70+ characters or not :\ but it is shortening effectively:

 

http://bp2.blogger.com/_ez9UzAGPSPU/Rhv0hLgdEXI/AAAAAAAAACo/IJrba0dnpr...

90

http://youtube.com/watch?v=vAkOti7pJJg...

38

http://www.fallingpixel.com/news/article.php?id=62...

50

http://www.consolisearch.com...

28

Link to comment
Share on other sites

You really should change your style and use the mysql_fetch_assoc() function to retrieve your rows. It will make your code cleaner (IMHO). Here is my take on your code (I also fixed your problem)

<?php
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM links ORDER BY linkid DESC";
$result=mysql_query($query);

while ($rw = mysql_fetch_assoc($result)) {
       $disp_url = (strlen($rw['linkurl']) > 70)?substr($rw['linkurl'],0,70) . '...':$rw['linkurl'];
       echo '
<span class="maintext">
<br>- <a href="'. $rw['linkurl'] . '">' . $disp_url . '</a><br>
</span>
';
       }
?>

 

Ken

Link to comment
Share on other sites

Ok The working code is:

 

<?

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Unable to select database");
$query="SELECT * FROM links ORDER BY linkid DESC";
$result=mysql_query($query);

$num=mysql_num_rows($result);

for ($i=0;$i<$num;$i++) {


$linkurl=mysql_result($result,$i,"linkurl");
$url='<a href="'.$linkurl.'">'.$linkurl.'</a>';

if (strlen($linkurl) >= 70)
{
   $url = '<a href="'.$linkurl.'">'.substr($linkurl, 0, 70).'...'.'</a>';
}
echo '
<span class="maintext">

'.$url.'<br>
'.strlen($linkurl).'<br>

</span>
';

}
?>

 

That produces exactly what I was after, thanks themistral and thanks for the tip kenrbnsn, ill take a look at that now.

 

Nathan

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.