regular express issue with 1 character string - php

I am allowing only alpha-numeric, _ & - values in string and removing all other characters. Its working fine but when string size 1 character (does not matter its alphabet or numeric or _ or -), I got empty value instead of single charter.
Here is sample code
$str = 1;
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
or
$str = 'a';
$str = preg_replace('/^[a-zA-Z0-9_-]$/', '', $str);
var_dump($str);
I have tested this multiple versions of PHP as well

You are removing any chars other than ASCII letters, digits, _ and - anywhere inside the string. You need to remove anchors and convert the positive character class into a negated one:
$str = preg_replace('/[^\w-]+/', '', $str);
See the PHP demo online and a regex demo.
Details
[^ - start of a negated character class
\w - a word char: letter, digit or _
- - a hyphen
] - end of the character class
+ - a quantifier: 1 or more repetitions.

Related

How to remove a character from a string only if it follows a number?

I have several rows of data that are in address format, I want to remove the house number from each address.
So far I have been able to remove the number using:
<?php
$string = '25a Test Lane';
if (preg_match("/[0-9]/", $string)) {
$string = preg_replace("/[0-9]/", "", $string);
}
?>
$string then becomes 'a Test Lane' - but how would I go about removing 'a' as well? Bearing in mind the 'a' could be any letter following a number. I'd want to remove any character that immediately follows the number (no space in between).
You can use
trim(preg_replace("/\b\d+[a-zA-Z]*\b/", "", $string))
trim(preg_replace("/\b\d+[a-zA-Z]?\b/", "", $string))
Here is the regex demo. NOTE: if you only want to allow a single letter after the number, replace * with ? in [a-zA-Z]*.
Details:
\b - a word boundary
\d+ - one or more digits
[a-zA-Z]* - zero or more ASCII letters
[a-zA-Z]? - one or zero ASCII letters
\b - a word boundary.
See the PHP demo:
$string = '25a Test Lane';
$string = trim(preg_replace("/\b\d+[a-zA-Z]*\b/", "", $string));
echo $string;
// => Test Lane

PHP - Remove all punctuation from the start and end of the string

I would like to trim all the punctuation and leave only letters or numbers at the beginning and at the end of the string. Any punctuation between letters and numbers should be retained.
This is what I tried from here PHP preg_replace: remove punctuation from beginning and end of string:
$str = '£££2343423 34234238& ';
$new = preg_replace('/^\PL+|\PL\z/', '', $str);
echo $new;
Kindly any recommendations, please?
You can use
$new = preg_replace('/^[^\p{L}0-9]+|[^\p{L}0-9]+\z/u', '', $str);
The regex matches
^[^\p{L}0-9]+ - any one or more chars other than Unicode letters and ASCII digits at the start of string
| - or
[^\p{L}0-9]+\z - any one or more chars other than Unicode letters and ASCII digits at the end of string.
See the PHP demo online and a regex demo.

preg_replace not replacing underscore

I want to allow only alpha numeric characters and spaces, so I use the following;
$name = preg_replace('/[^a-zA-z0-9 ]/', '', $str);
However, that is allowing underscores "_" which I don't want. Why is this and how do I fix it?
Thanks
The character class range is for a range of characters between two code points. The character _ is included in the range A-z, and you can see this by looking at the ASCII table:
... Y Z [ \ ] ^ _ ` a b ...
So it's not only the underscore that's being let through, but those other characters you see above, as stated in the documentation:
Ranges operate in ASCII collating sequence. ... For example, [W-c] is equivalent to [][\^_`wxyzabc].
To prevent this from happening, you can perform a case insensitive match with a single character range in your character class:
$name = preg_replace('/[^a-z0-9 ]/i', '', $str);
You have mistake in your expression. Last Z must be capital.
$name = preg_replace('/[^a-zA-Z0-9 ]/', '', $str);
^

Selective string reduction

I would like to know how to strip all non-alphanumeric characters from a string except for underscores and dashes in PHP.
Use preg_replace with /[^a-zA-Z0-9_\-]/ as the pattern and '' as the replacement.
$string = preg_replace('/[^a-zA-Z0-9_\-]/', '', $string);
EDIT
As skippy said, you can use the i modifier for case insensitivity:
$string = preg_replace('/[^a-z0-9_\-]/i', '', $string);
Use preg_replace:
$str = preg_replace('/[^\w-]/', '', $str);
The first argument to preg_replace is a regular expression. This one contains:
/ - starting delimiter -- start the regex
[ - start character class -- define characters that can be matched
^ - negative -- make the character class match only characters that don't match the selection that follows
\w - word character -- so don't match word characters. These are A-Za-z0-9 and _ (underscore)
- - hyphen -- don't match hypens either
] - close the character class
/ - ending delimiter -- close the regex
Note that this only matches hyphens (i.e. -). It does not match genuine dash characters (– or —).
Accepts a-z, A-Z, 0-9, '-', '_' and spaces:
$str = preg_replace("/[^a-z0-9\s_-]+/i", '', $tr);
No spaces:
$str = preg_replace("/[^a-z0-9_-]+/i", '', $tr);

Replace symbol if it is preceded and followed by a word character

I want to change a specific character, only if it's previous and following character is of English characters. In other words, the target character is part of the word and not a start or end character.
For Example...
$string = "I am learn*ing *PHP today*";
I want this string to be converted as following.
$newString = "I am learn'ing *PHP today*";
$string = "I am learn*ing *PHP today*";
$newString = preg_replace('/(\w)\*(\w)/', '$1\'$2', $string);
// $newString = "I am learn'ing *PHP today* "
This will match an asterisk surrounded by word characters (letters, digits, underscores). If you only want to do alphabet characters you can do:
preg_replace('/([a-zA-Z])\*([a-zA-Z])/', '$1\'$2', 'I am learn*ing *PHP today*');
The most concise way would be to use "word boundary" characters in your pattern -- they represent a zero-width position between a "word" character and a "non-word" characters. Since * is a non-word character, the word boundaries require the both neighboring characters to be word characters.
No capture groups, no references.
Code: (Demo)
$string = "I am learn*ing *PHP today*";
echo preg_replace('~\b\*\b~', "'", $string);
Output:
I am learn'ing *PHP today*
To replace only alphabetical characters, you need to use a [a-z] as a character range, and use the i flag to make the regex case-insensitive. Since the character you want to replace is an asterisk, you also need to escape it with a backslash, because an asterisk means "match zero or more times" in a regular expression.
$newstring = preg_replace('/([a-z])\*([a-z])/i', "$1'$2", $string);
To replace all occurances of asteric surrounded by letter....
$string = preg_replace('/(\w)*(\w)/', '$1\'$2', $string);
AND
To replace all occurances of asteric where asteric is start and end character of the word....
$string = preg_replace('/*(\w+)*/','\'$1\'', $string);

Categories