Welcome to WebmasterWorld Guest from 54.159.250.110

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

exclude characters from counting?

   
2:18 pm on Nov 10, 2009 (gmt 0)

5+ Year Member



Hello, I wanted to ask if you have a string like:

$my_s='ASRGREGTGTR----REGREGRE+++RRRRRR....';

is there a way to count the length of the string, excluding non-word characters (+,- or .)?

thank you

2:57 pm on Nov 10, 2009 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



You could remove the chars you don't want with a str_replace [php.net] and then just get the length [php.net] of the string afterward.

Off the top of my head I can think of like 3 other ways to do this, but the one above seems simplest.

4:41 pm on Nov 10, 2009 (gmt 0)

10+ Year Member



As eelixduppy said, just replace all the non-characters.

$my_s='ASRGREGTGTR----REGREGRE+++RRRRRR....';
$chars = preg_replace("/\W/i","", $my_s);

echo $chars ."<br>";
echo strlen($chars);

Cheers