473,804 Members | 3,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to test string whether it contains special characters

exoskeleton
104 New Member
please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid!

please help...TIA
Apr 18 '07 #1
21 21859
devsusen
136 New Member
I prefer preg_match() in such cases. Can u tell us what sort of special chars u like to search within names?

susen
Apr 18 '07 #2
tolkienarda
316 Contributor
please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid!

please help...TIA
one simple way to do that is with the strpos() function. here is a short tutorial of how it works
Expand|Select|Wrap|Line Numbers
  1. $username = 'frank!anderson';
  2. $pos = strpos($username, "!");
  3. if ($pos == -1)
  4. {
  5.    print 'dont put ! in your usernames you silly person';
  6. }
  7. else
  8. {
  9.    print 'everything is super cool and what not';
  10. }
i hope that helps other functions to look at include
strcmp()
strstr()
documentation can be found at php.net/function name
ex
http://www.php.net/strcmp

eric

oh and devsusen is right pregmatch is probably the most standard way to this. but is far to complicated for a simple mind such as myself so i always use these dumbed down versions.
Apr 18 '07 #3
exoskeleton
104 New Member
I prefer preg_match() in such cases. Can u tell us what sort of special chars u like to search within names?

susen
thank you sir devsusen.. :) but how can i add characters to preg_match() in order to find not just 1 special characters like the split function that only enclosed by "[ ]" like

Expand|Select|Wrap|Line Numbers
  1. split('[/.-]', $date);
in order to find not just 1 character in an expression or given string?

i tried like for example:

Expand|Select|Wrap|Line Numbers
  1. $string = '#richa@rd';
  2. preg_match('/!@#$%^&*()/','$string');
but cant able to find anything....and he can find something if like this:

Expand|Select|Wrap|Line Numbers
  1. $string = 'richard';
  2. preg_match('/chard/','$string');
Apr 19 '07 #4
exoskeleton
104 New Member
one simple way to do that is with the strpos() function. here is a short tutorial of how it works
Expand|Select|Wrap|Line Numbers
  1. $username = 'frank!anderson';
  2. $pos = strpos($username, "!");
  3. if ($pos == -1)
  4. {
  5.    print 'dont put ! in your usernames you silly person';
  6. }
  7. else
  8. {
  9.    print 'everything is super cool and what not';
  10. }
i hope that helps other functions to look at include
strcmp()
strstr()
documentation can be found at php.net/function name
ex
http://www.php.net/strcmp

eric

oh and devsusen is right pregmatch is probably the most standard way to this. but is far to complicated for a simple mind such as myself so i always use these dumbed down versions.
same with this sir... how can i add special characters to find not just 1.. yes this is possible if only 1 special character to find...

thank you sir
Apr 19 '07 #5
tolkienarda
316 Contributor
well my advice would be to go back to devsusen's idea. and what preg_match as i am sure you know uses regular expressions and a good tutorial on the complex world of regex is
http://weblogtoolscollection.com/regex/regex.php
and there are many more but this is php based and a good starting place
unfortunatly the functions i listed earlier can only look for one expression at a time. if you had an array of strings you were looking for say

you can use a loop
Expand|Select|Wrap|Line Numbers
  1. $string = 'adsf@ASDF!fdsa#FDSA%';
  2. $lookfor[0] = '!'
  3. $lookfor[1] = '@'
  4. $lookfor[2] = '#'
  5. $lookfor[3] = '%'
  6. $i = 0;
  7. while ($i < 4)
  8. {
  9.    $var = strpos($string, $lookfor[$i]);
  10.    if ($var != -1)
  11.    {
  12.       echo 'passed test for $lookfor[$i]';
  13.    }else
  14.    {
  15.        echo 'silly person none of those silly charcters';
  16.    }
  17.  
  18. }
now i typed this code into this forum directly so there might be a missing semi colon or paren but the basic logic will work.
(preg_match and regular expressions i have avoided for my entire programing carear because the confuse me to no end but if you can figure them out i urge you to go in that direction because they are extreamley powerful and i always feel stupid when i have a problem that could be easily solved with regex but i cannot do it without loops and if statements)

eric
Apr 19 '07 #6
exoskeleton
104 New Member
thank you sir... :)
Apr 20 '07 #7
devsusen
136 New Member
hi,

how can i add characters to preg_match() in order to find not just 1 special characters
To check multiple special characters u can use preg_match too. There u need to specify the characters u like to check using separator. I'm giving u an example.

Expand|Select|Wrap|Line Numbers
  1. preg_match('/[\s|\n|\t|*|@|&]/', $anyVariable); 
susen
Apr 20 '07 #8
tolkienarda
316 Contributor
hmm i realy need to learn that preg_match stuff it is way simpler than my loops and such.

eric
Apr 20 '07 #9
exoskeleton
104 New Member
thank you sir susen and sir eric...it really helps.....more power guys
Apr 21 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
4194
by: lawrence | last post by:
Someone on www.php.net suggested using a seems_utf8() method to test text for UTF-8 character encoding but didn't specify how to write such a method. Can anyone suggest a test that might work? Something that maybe gives 90% confidence that a given block of text is or is not UTF-8 encoded?
13
8762
by: usgog | last post by:
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two "i" and two "s". The function should also handle if String A and B have huge values, like two big dictionary. What's the best approach to achieve this with the best performance? what's the Big O then? I am thinking to put A and B into two...
12
4064
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and compare string lengths afterwards, you would know whether or not the line contained invalid characters. The new method is more than 10x faster than the standard 'if char in string' test! So - here's the code plus sample timings: ''' Translate...
6
3542
by: Mark | last post by:
Hi, My page has a table with many columns such that the right-side of the table gets chopped off when printed. I specify a table width of 100%, but otherwise no cell dimensions are specified. The culprits are 2 wide columns which contain e-mail addresses. I can get the page to fit entirely on the printer output if the browser would break the e-mail address string at the '@' symbol. What I've done for now is replaced the '@' in all...
17
8157
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found characters") Else Debug.WriteLine("Did NOT find characters!") End If
26
2600
by: alberto | last post by:
Hi. Im newbie in C language. I have a binary file with many character arrays of 50 character defined as char array But in some cases, many of these 50 characters are not being used. I would like to know how could I know how many characters are really being used in each array ? Thanks
14
4098
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
13
20786
by: titan nyquist | last post by:
How do you test a string to see if it contains special characters? I want to ensure that any names typed into my form has only letters (and maybe allow a dash and an apostrophe). I can loop RealName.Contains("..."), but there must be a more elegant solution.
4
2794
by: smartic | last post by:
how to test string whether it contains special characters -------------------------------------------------------------------------------- please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid ex for special characters (",',#,!,>,<,?) except any letters for other languages ?
5
11169
by: Sobin Thomas | last post by:
Hi All, I want to pass a string that contains many special characters (: \ . _ etc) to another page in my website through query string. In my project I have a Gridview control ,in which there is a hyperlink field.The Gridview 's datasource is set as a database table at runtime.The hyperlink filed's DataNavigateUrlFields is set to "RecordID" ,which is a field in my database table.The RecordID field contains many special characters.I...
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.