[Solved] preg_replace/string_replace symbol – or * to space

Introduction

The preg_replace/string_replace symbol – or * to space is a useful tool for replacing certain symbols with a space in a string. This can be useful for formatting text, removing unwanted characters, or making a string easier to read. This tutorial will explain how to use the preg_replace/string_replace symbol – or * to space in PHP. It will also provide some examples of how to use this function to achieve different results.

Solution

//Using preg_replace
$string = preg_replace(‘/[-*]/’, ‘ ‘, $string);

//Using str_replace
$string = str_replace(array(‘-‘, ‘*’), ‘ ‘, $string);


A few ways to do this.
String replace, as mentioned by Ashish.

$input  = "-**abcd1234-*---";
$output = str_replace(array('-', '*'), '', $input);
echo $output;

If you have a large number of characters to strip though, maybe this would be a little easier.

$input  = "-**abcd1234-*---";
$remove = "-*";
$output = str_replace(str_split($remove), '', $input);
echo $output;

And of course, you can use regex.

$input  = "-**abcd1234-*---";
$output = preg_replace('/[*-]/', '', $input);
echo $output;

0

solved preg_replace/string_replace symbol – or * to space


The preg_replace and str_replace functions are useful for replacing symbols such as the asterisk (*) or hyphen (-) with a space. This can be useful when you want to separate words or phrases that are joined together with a symbol.

To use preg_replace, you can use the following code:

$string = preg_replace('/[*-]/', ' ', $string);

This code will replace any asterisk or hyphen with a space.

To use str_replace, you can use the following code:

$string = str_replace(array('*', '-'), ' ', $string);

This code will replace any asterisk or hyphen with a space.

Both of these functions can be used to replace symbols with a space, allowing you to separate words or phrases that are joined together with a symbol.