exclude characters from counting?
Hello, I wanted to ask if you have a string like:
is there a way to count the length of the string, excluding non-word characters (+,- or .)?
First thing that comes to mind (untested):
"\W" is "any non-word character," so it would count a-ZA-Z0-9 and underscores. If you wanted it more restrictive, you would need a more specific regexp, such as '/[^a-ZA-Z]/' to replace anything that is not a letter.
$count = strlen(preg_replace('/\W/', '', $string);
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)