DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Regular Expression

By: | 18 Feb 2009 4:25 pm

Looking for a little help with regular expressions that I suck at... :(

I have a page where the user can enter a number of email addresses separated by white space, comma, or semi-colon. I found some code on the PHP site to split it by comma and/or whitespace, but I wanted to add semicolon. It looks like so:

$email = preg_split("/[s,]+/", $entry);

So I tried:

$email = preg_split("/[s,;]+/", $entry);

and it seemed to ignore the semicolon. Any help would be appreciated. Thanks.

Comments

Looking for a little help with regular



As in your example, $email will be an array. Try split() which can accept regular expressions as well:

$email = split("[; ,]+", $entry);

By: | 18 Feb 2009

I just tried the following and it worked fine for me:

billy@devserver:~$ less test.php

<?php
$emails = `john@... ; show@... , qhas@...
asdjklf@...`;
print_r(preg_split(`/[s,;]+/`,$emails));
?>

billy@devserver:~$ php test.php
Array
(
[0] => john@...

[1] => show@...
[2] => qhas@...
[3] => asdjklf@...
)

If you are not getting this result then there is a bug somewhere else in your code or perhaps (yet less likely) there is a bug in your version of php.

By: | 18 Feb 2009

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move