473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Please help on this preg_match

290 100+
Hi,

I am using the following regex to check for valid email
addresses, but I am getting errors.

Expand|Select|Wrap|Line Numbers
  1. if (preg_match('\^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$\',$regex)){
  2.        echo "VALID";
  3.      }
  4. else {
  5.      echo "INVALID";
  6.     }
The errors that I get are:

Warning: Unexpected character in input: ''' (ASCII=39) state=1 in /home/guru54gt5/public_html/im/regex.php on line 6

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/guru54gt5/public_html/im/regex.php on line 6

Parse error: syntax error, unexpected '^', expecting ')' in /home/guru54gt5/public_html/im/regex.php on line 6

Line 6 refers to the if stmt line in my script.

I took the example from a tutorial and I thought that I understood everything, but I can not
see why this is causing errors :)


PS I changed the single quotes to double:
if (preg_match("\^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$\",$regex)){

and now I get just one error:
Parse error: syntax error, unexpected T_STRING in /home/guru54gt5/public_html/im/regex.php on line 7

But, again, I do not see why
Thanks for any help.
Dec 17 '09 #1
5 3106
jeddiki
290 100+
OK,
I have seen my mistake,
I changed backslashes to forward slashes at the perimeters.
Now it works OK

BUT now I am confused by my results.

I am using a simple form to input to this script:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. if( isset($_POST['x_regex'])) {
  3.    $regex = "{$_POST['x_regex']}";
  4.      if (preg_match("/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/",$regex)){
  5.        echo "VALID";
  6.          }
  7.         else {
  8.           echo "INVALID";
  9.         }
  10. }
  11. else {
  12. require("regex_fm.php");
  13. }
  14. ?>

When I enter this: fred@fgf.gh4.co
It comes up as valid - but the number 4 should
get it thrown out - why doesn't it ?

and how can I make it do it ?

Thanks
Dec 17 '09 #2
Dormilich
8,658 Expert Mod 8TB
why should the number 4 get thrown out?
Dec 18 '09 #3
jeddiki
290 100+
OK - you are correct - it shouldn't

So that validation is OK

Now I am also doing a URL validation:
This is more complicated because I am using preg_replace

I am trying to find a url withing the text and convert it to a
hyper link.

So I need to collect the parts of the url and stick them
together

First bit is to check to see if http or https
If the http is not there I need to add it at the beginning of the result

So my code is now

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. if( isset($_POST['x_regex'])) {
  3.    $regex = "{$_POST['x_regex']}";
  4.      $new_url = preg_replace("@((https?://)?([-\w]+\.)+(-\w{2,})+(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)?)@", '<a href="$1">$1</a>', $regex);
  5.       $new_url ="<a href=".$new_url."<a>"; 
  6.      echo "<br><br>URL:  $new_url";
  7.      echo "<br><br><a href=\"http://www.expert-world.com/im/regex.php\">Try Another<a>";
  8.      exit;
  9. }
  10. else {
  11. require("regex_fm.php");
  12. }
  13. ?>
  14.  
I am a bit stuck.

The testing page is here

BTW - I got this validation from the a google search can
someone tell me why this included: (:\d+)
what does that colon do ?
Dec 18 '09 #4
kovik
1,044 Expert 1GB
You should probably take the time to actually teach yourself regex. A good learning tool is The Regex Coach. It lets you see how the regex works as you type it.

Anyway, the colon is not a special character. It is a port number, like http://www.google.com:80/ (80 is the public port).
Dec 18 '09 #5
Dormilich
8,658 Expert Mod 8TB
(80 is the public port)
it is rather the default (HTTP) server port. if you have configured your server correctly, you can use any valid port you want.
Dec 18 '09 #6

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

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
0
by: awebguynow | last post by:
I ran across this code, and it kind of made me nervous: (as an email validator) if ( !preg_match("/.*\@.*\..*/", $_POST) | preg_match("/(\)/", $_POST) ) 1) from bitwise experience with "C",...
4
by: squash | last post by:
I have a string equal to 'www/' that I want to use in a preg_match. Php keeps giving me the warning: Warning: preg_match(): Unknown modifier '/' How can I escape the string so the / in www/ is...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
9
by: Synapse Syndrome | last post by:
Hi I've been given what I am told is a PHP script to be used on my server. I do not know any PHP. I am trying to use a feature of a program called ArchiCAD. This feauture allows CAD drawing...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.