Replacing everything but letters and numbers...

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Replacing everything but letters and numbers...

Post by Mr Tech »

I've seen this so many times but can't remember where... because it's such a logn search term I get everything but what I want...

Can anyone give a link, code, tutorial on how to remove everything except letters and numbers from some text?

Cheers 8)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Mess with this regex:

Code: Select all

function isAlphaNumericOrSlash($str){
	
	if (preg_match('/[\W]/i', $str)) 
		return false;
	else
		return true;	
}
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

I'm not that smart when it comes to REGEX... Have you got a URL or piece of code that replaces it and returns the value with just the numbers and letters?

Thanks for your help.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

function aphanumerify($string) {

    $string = preg_replace('/[^a-z0-9]/i', '', $string);
    return $string;

}
\W wont remove underscores.

http://regular-expressions.info
\W wont remove underscores.

http://regular-expressions.info[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info
function aphanumerify($string) {

$string = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info
}


\W wont remove underscores.

http://regular-expressions.infooring;

}


\W wont remove underscores.

http://regular-expressions.info

}


\W wont remove underscores.

http://regular-expressions.inforing);
return $string;

}


\W wont remove underscores.

http://regular-expressions.infotring = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.infoemove underscores.

http://regular-expressions.info $string = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info

\W wont remove underscores.

http://regular-expressions.infoerify($string) {

$string = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.infostring = preg_replace('/ї^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info $string = preg_replace('/ї^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.infoumerify($string) {

$string = preg_replace('/ї^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.infohp]

\W wont remove underscores.

http://regular-expressions.infotion aphanumerify($string) {

$string = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}
/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info;^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressionpressions.info
function aphanumerify($string) {

$string = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info'/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info
function aphanumerify($string) {

$string = preg_replace('/[^a-z0-9]/i', '', $string);
return $string;

}


\W wont remove underscores.

http://regular-expressions.info
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Awesome, thanks!
Post Reply