timecatcher Posted February 28, 2009 Share Posted February 28, 2009 Hey ok well i've looked for tutorials everywhere and I don't seem to be able to find one that doesn't use eregi(). None of the tutorials I've seen actually explain this and the PHP.net website had a terrible description for a newby like myself, so I was wondering if anyone would mind giving me some basic tips on validation here as your helps always great! Thanks TC. P.S This is validation for my register script by the way. Link to comment https://forums.phpfreaks.com/topic/147294-solved-validation/ Share on other sites More sharing options...
cwarn23 Posted February 28, 2009 Share Posted February 28, 2009 Well there is the strlen function like as follows: if (strlen($data)>0) //check if string length greater than 0 Also preg_match_all, preg_replace, preg_match which are simular to eregi. Other than that there isn't that much that can validate a string. Link to comment https://forums.phpfreaks.com/topic/147294-solved-validation/#findComment-773195 Share on other sites More sharing options...
timecatcher Posted February 28, 2009 Author Share Posted February 28, 2009 I don't understand what preg_match (or eregi) is or how to use it, and I've checked the php.net site and it doesn't seem to make any sense how they explain it. TC. Link to comment https://forums.phpfreaks.com/topic/147294-solved-validation/#findComment-773198 Share on other sites More sharing options...
cwarn23 Posted February 28, 2009 Share Posted February 28, 2009 I shall explain the basics and you can learn on from there. First of all, preg_replace, preg_match, preg_match_all, they all use the same syntax. And that syntax I shall explain the basics. Note that in these examples, the test string such as a sentence to replace or match is in the variable $data. Say you wanted to match a word. You would use the following: preg_match('/myword/i',$data); Now you will notice straight away that it is surrounded by forward slashes and on the right hand side is the letter i outside the forward slash. That is known as the eliminator. It determins if the string is case sensitive or if case insensitive. But if you try to add a forward slash inside the string to match or replace, you will notice an eliminator error is reported. To solve that you need to escape all forward slashes except the two that surround the string like in the following example: preg_match('/aaa\/bbb/i',$data); So in the above example, that is actually a backslash before the forward slash in the middle of the string so it doesn't get mixed up with the eliminator. Then you may say, what if you wanted to make it case sensitive, the following will make the above case sensitive: preg_match('/aaa\/bbb/is',$data); Hope your not getting confused yet. Then a common thing that you may need to do is match multiple words. That is when brackets come in and a special or function. So below will match 2 words case sensitive: preg_match('/(Word|word)/is',$data); That will only accept the phrase Word or word and is case sensitive. To explain more on that, the column found beside the backspace key | acts like the or event in an if function. Then the brackets determin what the barrier is for that or event. Now say you wanted to match a column symbol | which was used in the or event. You would need to escape it like the forward slash and would be like follows: preg_match('/\|/is',$data); Now what if you wanted to make a string match but have an optional s at the end. The following will do the job. preg_match('/word(s)?/i',$data); That will match both the phrase word and words case insensitive. Once again if you want to match a question mark instead of questioning if the previous symbol exists then you would need to use the following to match "words?" preg_match('/words\?/i',$data); I hope that tutorial helps and let me know if you need more. Link to comment https://forums.phpfreaks.com/topic/147294-solved-validation/#findComment-773208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.