-6

I want to replace space with "|" and i want only numbers and alphabets and star(*) in the sting

And i have tried str_replace() funtion but it replace the space with | , but i want to validate

echo $text= str_replace(' ','|', "This is some $123 Money");

output

This|is|some|$123|Money

what i expect is

This|is|some|123|Money

I don't want any other special characters in my output

Any Suggestions

Thanks in Advance

Weedoze
  • 13,683
  • 1
  • 33
  • 63
Abid
  • 344
  • 1
  • 4
  • 21
  • Do you want the value of a variable? In PHP variable names cannot start with numbers... that's why PHP is assuming 123 is not a variable – Fredster Aug 01 '17 at 12:18

2 Answers2

2

You can use preg_replace to remove all special characters from your string.

Example

echo $text= str_replace(' ','|', preg_replace('/[^A-Za-z0-9 ]/', '',"This is some $123 Money"));

Output

This|is|some|123|Money
shubham715
  • 3,324
  • 1
  • 17
  • 27
0

Here is the working demo for you: https://3v4l.org/M456J

<?php
$s = "This is some $123 Money";
$result = preg_replace("/[^a-zA-Z0-9]+/", "|", $s);
echo $result;

Output:

This|is|some|123|Money
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35