homepage Welcome to WebmasterWorld Guest from 23.20.220.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
exclude characters from counting?
ktsirig




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

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

 

eelixduppy




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

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.

JohnCanyon




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved