Jump to content

need to remove asteriks * from a string


s0c0

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/63586-need-to-remove-asteriks-from-a-string/
Share on other sites

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.

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.