How to allow specific values in my string [duplicate] - php

This question already has answers here:
Remove all special characters from a string [duplicate]
(3 answers)
Closed 5 years ago.
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

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

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

Related

How to add a space between words? [duplicate]

This question already has answers here:
php explode at capital letters?
(4 answers)
Closed 1 year ago.
I have a string that has words without any spaces between them. How do I add a space between each word? Example: my string has combined words FinancialTimes and I would like it to show as Financial Times
<?php $word = 'FinancialTimes';?>
You could use preg_replace with a regex option:
$word = "FinancialTimes";
$output = preg_replace("/(?<=[a-z])(?=[A-Z])/", " ", $word);
echo $output; // Financial Times

Consecutive find/replace with regexp [duplicate]

This question already has answers here:
How to replace all occurrences of two substrings with str_replace()?
(3 answers)
Closed 5 years ago.
Say I want to erase any quote from this string, and then replace spaces with '&', in php. I could easily do with 2 consecutive preg_replace or the like, but how to do it in only 1 passage?
" columns="4" link="file" ids="280,281,282,283,284,285,286,287,288""
to:
columns=4&link=file&ids=280,281,282,283,284,285,286,287,288
You don't need regex for that. str_replace() should work fine:
$string = 'columns="4" link="file" ids="280,281,282,283,284,285,286,287,288"';
$replaced = str_replace([' ', '"'], ['&', ''], $string);
Demo: https://3v4l.org/gHHMp

How to strip non-alphabetic, spaces and numeric characters from a string? [duplicate]

This question already has answers here:
How to remove non-alphanumeric characters?
(7 answers)
Closed 8 years ago.
How can I use php and regex to strip all non-alphabetic, spaces and all numeric from a string?
I've tried this:
$input = "Hello - World 12";
$output = preg_replace("/[^a-zA-Z0-9\s]/", "", $input);
Wanted output: HelloWorld
You could simply use,
$output = preg_replace("~[\W0-9_]~", "", $input);
$output = preg_replace('/[\W\d_]/i', '', $input);
Use this expression:
$answer = preg_replace("/[^A-Za-z]/", '', $input);
This will remove any character which are not from:
A-Z
a-z
To remove any of white spaces:
$string = preg_replace('/\s+/', '', $answer);
Remove the 0-9 and \s from your regex like so:
$input = "Hello - World 12";
$output = preg_replace("/[^a-zA-Z]/", "", $input);
Now you are checking for every character which is not (^) lowercase a-z or uppercase a-z. Than you are replacing that character with nothing "".

PHP string control on chars and numbers only [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Function to return only alpha-numeric characters from string?
Starting from $string = "hey hello 9times-%&";
I would like to replace all the chars that are NOT numeric[0-9] or [a-z,A-Z] type.
Is there a good method to show this process control?
EDITED
i forgot that i need to leave blank space bar blank spaces, i mean:
"hey &/k" must return as "hey k" and NOT as "heyk"
<?php
$string = "hey hello 9times-%&";
$string = preg_replace('/[^0-9A-Z\s]+/i', '', $string);
echo $string;
?>
preg_replace('/[^ \w]+/i', '', $string);
That will work as well. See the codepad example.
What about preg_replace:
$clean = preg_replace('/[^0-9a-z\s]/i','',$input);

Trim multiple characters using php [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 12 months ago.
How to trim multiple characters at begin and end of string.
string should be something like {Hello {W}orld}.
i want to trim both { and } at begin and end.
don't want to use multiple trim function.
Use the optional second argument to trim which allows you to specify the list of characters to trim:
<?php
$str = "{Hello {W}orld}";
$str = trim($str, "{}");
echo "Trimmed: $str";
Output:
Trimmed: Hello {W}orld
Is there always a character at the beginning and at the end, that you want to remove? If so you could just use the following:
<?php
$string = '{Hello {W}orld}';
echo substr( $string, 1, strlen( $string )-2 );
?>
See: http://codepad.org/IDbG6Km2

Categories