Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 982373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:41:10+00:00 2026-05-16T04:41:10+00:00

I want to count how many characters a certain string has in PHP, but

  • 0

I want to count how many characters a certain string has in PHP, but I cant get it to work.

I did a var_dump and this is what I got:

string(23) "Children’s Center"

It seems like ' gets translated to ’s

This makes it impossible to get the actual character count. I tried using html_entity_decode but it did not help.

Anyone?

EDIT:
My function looks like this now:

function make_shorter($string, $maxlength)
{
 $string = stripslashes($string);   
 $string = html_entity_decode($string);
 if ( mb_strlen(utf8_decode($string)) > $maxlength)
  return substr($string,0,$maxlength).'...';
 else
  return $string;
}

I can’t change how the data gets in to the system. I can only modify it’s output.

(It’s a wordpress site)

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:41:10+00:00Added an answer on May 16, 2026 at 4:41 am

    It seems like ' gets translated to ’s

    That’s the HTML character reference for the single right smart-quote, ’, which some people also use to represent an apostrophe (especially those typing in MS Word). At some prior point in your processing, something has applied htmlentities() to your data.

    HTML-escaping should only be carried out at the output stage, when inserting text into an HTML page. So look back and see if you’ve got a function doing something stupid like calling htmlentities() over every entry in the $_POST/$_GET/$_REQUEST array. This is a common but completely bogus way to try to prevent XSS attacks. If you see it, you are at some point going to need to take it out and go through your templates adding proper HTML-escaping around every time you drop a variable into HTML.

    Either way, use htmlspecialchars() instead of htmlentities() and it’ll leave the non-ASCII characters like ’ alone.

    html_entity_decode() definitely should undo the above encoding, leaving you with a raw string. ’ might still count as either one or two bytes, though, depending on what encoding you decode into. If you want to count characters properly and you have UTF-8 strings, you will need mb_strlen().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table which has many fields but i want to get count
I want to count how many times each user has rows within '5' of
I want to count how many times a user watches a Product. Product has
I want to count how many times i built on VS.NET . I have
I want to count how many members of an iterable meet a given condition.
I want to count the numbers of hard tab characters in my documents in
I want to count how many divs with the class .tool that contain the
My table had 3 fields: id and unit. I want to count how many
I want to count the number of upper case letters in a string using
I want to count how many cells that have a value given that a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.