Hi,

I'm looking for help with my description meta.

I have set it for a maximum length of 150 characters which works fine, but does anybody know how I can set it so that it stops after the first white space after 150 characters ie so that only full words are shown.

The code I am using is below:

$result = mysql_query("SELECT * FROM database WHERE id = 7573");
while ($row = mysql_fetch_array($result)) {

echo "<HTML>\n";
echo "<HEAD>\n";
echo "<title>".$row{'Name'}.", ".$row{'Location'}.", ".$row{'Country'}.\"</title>\n";
[B]echo "<meta name=\"description\" content=\"".$row{'Name'}." - ". substr($row['Description'],0,150) ."\">\n";[/B]
echo "</head>\n";

Thanks for your help

echo "<meta name=\"description\" content=\"".$row{'Name'}." - ". substr($row['Description'],0,strpos($row['Description'],' ',150)) ."\">\n";

I think that this will break the string at the first whitespace after 150 characters

anyone seen the fingers crossed icon :)

Hi,

Thanks very much for your help. This works perfectly. I have been trying for hours to figure this out!

echo "<meta name=\"description\" content=\"".$row{'Name'}." - ". substr($row['Description'],0,strpos($row['Description'],' ',150)) ."\">\n";

I think that this will break the string at the first whitespace after 150 characters

anyone seen the fingers crossed icon :)

Sorry, my mistake, for some reason I thought that strrpos was used, now I see it is strpos, so everything should work

The only thing is if the description is shorter than 150 chars, this function may cause error because strpos cannot have value of offset that is greater than length of string
you need to first check that strlen($row) > 150

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.