[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP string  (PREG?)  how to replace several punctuation chars with space

Posted on 2009-12-26
6
Medium Priority
?
451 Views
Last Modified: 2012-05-08
is there an easy single call to a function that could replace the any  non alphanumeric  with a simple space

thus the following...  
the,quick.brown:fox!jumped%over(the*lazy#etc2
 would yield
the quick brown fox jumped over the lazy etc2

thanks
0
Comment
Question by:willsherwood
  • 2
  • 2
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
gamebits earned 1000 total points
ID: 26126160
$filename = "the,quick.brown:fox!jumped%over(the*lazy#etc2";

$illegalchars_type_1 = array(".",'"',":","%","!","*","#","|"," ",",");

$filename = str_replace($illegalchars_type_1,"_",$filename);

echo "$filename";
0
 
LVL 28

Expert Comment

by:gamebits
ID: 26126161
Sorry should be this

$filename = str_replace($illegalchars_type_1," ",$filename);
0
 
LVL 14

Assisted Solution

by:TedInAK
TedInAK earned 1000 total points
ID: 26126169
This is from http://newsourcemedia.com/blog/php-remove-non-alphanumeric-characters/

<?php

$string = Here! is some text, and numbers 12345, and symbols !£$%^&;

$new_string = preg_replace(/[^a-zA-Z0-9\s]/, , $string);

echo $new_string

?>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Closing Comment

by:willsherwood
ID: 31670136
thanks all!
0
 
LVL 14

Expert Comment

by:TedInAK
ID: 26126171
Whoops...try this instead:
<?php

$string = Here! is some text, and numbers 12345, and symbols !£$%^&;

$new_string = preg_replace(/[^a-zA-Z0-9\s]/, " " , $string);

echo $new_string

?>

Open in new window

0
 

Author Comment

by:willsherwood
ID: 26126176
thanks i assumed quotes for BOTH parameters - they are hidden in your original message

thanks again
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question