Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Pattern Matching

Status
Not open for further replies.

Actorial

Programmer
Aug 23, 2006
38
0
0
US
Hi There,

I have question about pattern matching. What I'm trying to do is replace any characters in a string that are not alphanumerics or spaces and these characters:

,.?!"-[]/'

I have this line going, it only replaces alphanumerics and spaces though:

$string = ereg_replace("[^[:space:]a-zA-Z0-9]", "", $string);

but that as far as I get without screwing it up. Any help?
 
also would like to allow @,

so the list of characters I want to allow is

,.?!"-[]/'@

thanks!
 
this might work faster (not tested, though)
Code:
<?
$html = "some long string # ";
$len = strlen($html);
$replacement = "W"; //single character
for ($i=0; $i<$len; $i++){
	if ((( ord($html{$i})<62) 	&& 	(ord($html{$i}) !== 32) ) ||   ( ord($html{$i}) > 122)){
		$html{$i} = $replacement;
	 }
}
echo $html;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top