Jump to content

[SOLVED] str_replace


andz

Recommended Posts

Store each one of the characters you want to remove in an array.

 

<?php
$array = array('.',',','!','@','#','$','%','^','&','*','(',')','/','?');
$str = ' some string ';

$replace_string = str_replace($array, '', $str);

 

With replacing a space with underscore, I think you can manage that.

Link to comment
https://forums.phpfreaks.com/topic/160928-solved-str_replace/#findComment-849280
Share on other sites

<?php
$string = "This is a simple, most blah, blah!!!.";
$string = str_replace(" ", "_", $string);//replcae the spaces with an underscore
$remove_array(".", ",", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "/", "?");
$string = str_replace($remove_array, "", $string);//replaces the values from $remove_array with nothing
echo $string;

 

Virtually identical to what Ken gave you. Does the space to udnerscore as well.

 

You might find a simple regular expression better suited for this.

Link to comment
https://forums.phpfreaks.com/topic/160928-solved-str_replace/#findComment-849282
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.