Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.