s0c0 Posted August 6, 2007 Share Posted August 6, 2007 I am returning a string from PHP script that queries a mysql database to javascript which then later seperates a bunch of values with an asteriks and passes them back to a PHP script to be exploded on that character. So when I'm returning products with asteriks * in them its bound to cause some problems. I have tried using the following preg_replace("/*/", "", $row[name]) but I get the following error: Compilation failed: nothing to repeat at offset 0 in somescript.php on line 143 I have tried using trim and ereg_replace as well, but with no luck. I have also tried using javascripts replace method, but it does not do a very good job of this. Any ideas on how I can solve this problem? Quote Link to comment https://forums.phpfreaks.com/topic/63586-need-to-remove-asteriks-from-a-string/ Share on other sites More sharing options...
GingerRobot Posted August 6, 2007 Share Posted August 6, 2007 Perhaps i've missed the point. Try: <?php $str = 'A string with an * in it'; $str = str_replace('*','',$str); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/63586-need-to-remove-asteriks-from-a-string/#findComment-316849 Share on other sites More sharing options...
s0c0 Posted August 6, 2007 Author Share Posted August 6, 2007 No you have not missed the point, this is an acceptable solution. However I was hoping to use preg_replace since you can put the characters youd liked removed in an array, but Im lazy to ill use this instead. Quote Link to comment https://forums.phpfreaks.com/topic/63586-need-to-remove-asteriks-from-a-string/#findComment-316861 Share on other sites More sharing options...
GingerRobot Posted August 6, 2007 Share Posted August 6, 2007 You can use arrays of matches with the str_replace function too: <?php $str = 'A string with an * and a [ in it'; $remove = array('*','['); $str = str_replace($remove,'',$str); echo $str; ?> Also, the reason why your preg_match attempt was not working is that an asterix is a special character which defines the number of times a patter occurs. You need to escape it. So this would have worked: <?php $str = 'A string with an * in it'; $str = preg_replace('/\*/','',$str); echo $str; ?> However, for simple string replaces like this, use str_replace as it is faster. Quote Link to comment https://forums.phpfreaks.com/topic/63586-need-to-remove-asteriks-from-a-string/#findComment-316885 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.