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

  • Home
  • SEARCH
  • 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 6801253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:04:38+00:00 2026-05-26T19:04:38+00:00

The str_word_count() function returns an array that holds all words in a string. It

  • 0

The str_word_count() function returns an array that holds all words in a string. It works great, except when using special characters. In this case, the php script receives the string via querystring:

When i open:
http://localhost/index.php?q=this%20wórds

header('Content-Type: text/html; charset=utf-8');
print_r(str_word_count($_GET['q'],1,'ó'));

Instead of returning:

[0] this
[1] wórds

…it returns:

[0] this
[1] w
[2] rds

How could this function support those special characters that are being sent through querystring?

Update – it worked out just fine by using mario‘s solution:

function sanitize_words($string) {
    preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u",$string,$matches,PREG_PATTERN_ORDER);
    return $matches[0];
}
  • 1 1 Answer
  • 0 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-26T19:04:39+00:00Added an answer on May 26, 2026 at 7:04 pm

    Not sure if that third parameter is sufficient to make str_word_count work for non-ASCII symbols. It probably only works with Latin-1 if anything.

    As alternative you could count the words with a regex however:

    $count = preg_match_all('/\pL+/u', $_GET['q'], $matches);
    

    This works for UTF-8 at least. To fully replicate str_word_count you might need [\pL']+ eventually.

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

Sidebar

Related Questions

str_replace('Ê','',$line); Ain't working. Is there some special string that represents that?
Initial javascript to get a string/array: <script type=text/javascript> $(document).ready(function() { $(#table).tableDnD({ onDrop: function(table, row){
I need to count words in a string using PHP or Javascript (preferably PHP).
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
This is a homework assignment, just for all that want to know. I'm writing
I'm attempting to create a recursive function that outputs a vector of strings that
Ok guys, i'm very beginner and trying to enter string to a char array
I am trying to make an OCaml function that addsthe number of 'a's in
string str = one three; string::iterator it; string add = two ; Lets say
function escCtrlChars(str) { return str.replace(/[\0\t\n\v\f\r\xa0'!-]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });

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.