Jump to content

Substr Not Working As Expected


stevesimo

Recommended Posts

Hi, I am trying to use SUBSTR to simply output the first 50 letters of a string.  Here is my code

 

$showinfo = $show['showinfo'];
$info = substr($showinfo, 0, 50);
echo $info;
[code]

The problem is that my output is only 30 chars (see below for example output)
An exhibition of the best loca
One of the largest regional we

Can anyone see what I am doing wrong?

Thanks, Steve

[/code]

Link to comment
https://forums.phpfreaks.com/topic/81487-substr-not-working-as-expected/
Share on other sites

Yes, if I dont use substr and simply output the value there is always more than say 100 chars.  I have set the database field type as text although I havent specified any other parameters.  When I check the database using PhpMyAdmin the values are all stored.  HTML is also stored within some of the tags.  Could this be causing the problem?

If there are HTML tags in the string, you should use the htmlentities() function when you echo the value to ensure that the tags are not interpreted.

 

<?php
echo htmlentities(substr($show['showinfo'],0,50),ENT_QUOTES);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

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