Posts Tagged ‘regex’

Adding space before every capital letter in a string via PHP preg_replace() and regex

Monday, April 26th, 2010

Hello again everyone, I found this great regex which is very effective in adding a space before every capital letter.  You can think a lot of things where to use this function.

So without further blah blah,  see the function below :)

<?php
 
function SpaceBeforeCapital($string){
	return preg_replace('/(?<!\ )[A-Z]/', ' $0', $string);
}
 
$SampleString = 'ReginValerianoWebDeveloper';
 
echo SpaceBeforeCapital($SampleString);
 
?>

Result string below:

Regin Valeriano Web Developer

NOTE: Im not the original writer of the regex I just found it on the web, I think its interesting, gave it a try and it works :) now made as a function.

Validating email via regular expression

Tuesday, February 23rd, 2010

You have an online form.

You want to validate every field,  especially email input fields.

You do not want just check if its has a value of not, but also if its in the right format (e.g. email@email.com).

If you answered yes to all of these then the function below if perfect for you.

Check it out…

function validate_email($email){
     if(!eregi("^([[:alnum:]]|_|\.|-)+@([[:alnum:]]|\.|-)+(\.)([a-z]{2,4})$", $email)){
	  return $error=1;
     }
}

Fell free to abuse…