I have this function that returns me an friendly url string.
public static function getUrlFriendlyString($str) {
// convert spaces to '-', remove characters that are not alphanumeric
// or a '-', combine multiple dashes (i.e., '---') into one dash '-'.
$_str = preg_replace("[-]", "-", preg_replace("[^a-z0-9-]", "",
strtolower(str_replace(" ", "-", $str))));
return substr($_str, 0, 40);
}
Anyway, if I have for example this String:
"Product with vitamins, protein, and a lot of good stuff"
The resulting string is:
"product-with-vitamins,-protein,-and-a-lot-of-good-stuff"
As you can see it doesn't remove the commas from the string :/ and my knowledge about regular expressions is null.
You left out the delimiters around the regexp, so it used [ and ] as the delimiters. As a result, they weren't being treated as the character class operators.
If you want to compress multiple - into one, the regexp is /-+/, not [-].
public static function getUrlFriendlyString($str) {
// convert spaces to '-', remove characters that are not alphanumeric
// or a '-', combine multiple dashes (i.e., '---') into one dash '-'.
$_str = preg_replace("/-+/", "-", preg_replace("/[^a-z0-9-]/", "",
strtolower(str_replace(" ", "-", $str))));
return substr($_str, 0, 40);
}
Related
I want to write a PHP function that keeps only a-z (keeps all letters as lowercase) 0-9 and "-", and replace spaces with "-".
Here is what I have so far:
...
$s = strtolower($s);
$s = str_replace(' ', '-', $s);
$s = preg_replace("/[^a-z0-9]\-/", "", $s);
But I noticed that it keeps "?" (question marks) and I'm hoping that it doesn't keep other characters that I haven't noticed.
How could I correct it to obtain the expected result?
(I'm not super comfortable with regular expressions, especially when switching languages/tools.)
$s = strtolower($s);
$s = str_replace(' ', '-', $s);
$s = preg_replace("/[^a-z0-9\-]+/", "", $s);
You did not have the \- in the [] brackets.
It also seems you can use - instead of \-, both worked for me.
You need to add multiplier of the searched characters.
In this case, I used +.
The plus sign indicates one or more occurrences of the preceding element.
I want to disallow all symbols in a string, and instead of going and disallowing each one I thought it'd be easier to just allow alphanumeric characters (a-z A-Z 0-9).
How would I go about parsing a string and converting it to one which only has allowed characters? I also want to convert any spaces into _.
At the moment I have:
function parseFilename($name) {
$allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
$name = str_replace(' ', '_', $name);
return $name;
}
Thanks
Try
$name = preg_replace("/[^a-zA-Z0-9]/", "", $name);
You could do both replacements at once by using arrays as the find / replace params in preg_match():
$str = 'abc def+ghi&jkl ...z';
$find = array( '#[\s]+#','#[^\w]+#' );
$replace = array( '_','' );
$newstr = preg_replace( $find,$replace,$str );
print $newstr;
// outputs:
// abc_defghijkl_z
\s matches whitespace (replaced with a single underscore), and as #F.J described, ^\w is anything "not a word character" (replaced with empty string).
preg_replace() is the way to go here, the following should do what you want:
function parseFilename($name) {
$name = str_replace(' ', '_', $name);
$name = preg_replace('/[^\w]+/', '', $name);
return $name;
}
[^\w] is equivalent to [^a-zA-Z0-9_], which will match any character that is not alphanumeric or an underscore. The + after it means match one or more, this should be slightly more efficient than replacing each character individually.
The replacement if spaces with spaces does not require the might of the regex engine; it can wait out the first round of replacements.
The purging of all non-alphanumeric characters and underscores is concisely handled by \W -- it means any character not in a-z, A-Z, 0-9, or _.
Code: (Demo)
function sanitizeFilename(string $name): string {
return preg_replace(
'/\W+/',
'',
str_replace(' ', '_', $name)
);
}
echo sanitizeFilename('This/is My 1! FilenAm3');
Output:
Thisis_My_____1_FilenAm3
...but if you want to condense consecutive spaces and replace them with a single underscore, then use regex. (Demo)
function sanitizeFilename(string $name): string {
return preg_replace(
['/ +/', '/\W+/'],
['_', ''],
$name
);
}
echo sanitizeFilename('This/has a Gap !n 1t');
Output:
Thishas_a_Gap_n_1t
Try working with the HTML part
pattern="[A-Za-z]{8}" title="Eight letter country code">
I need to to create url from various strings.
So trying to replace all non-alphanumeric characters and all of their combinations - with a hyphen character (-)
$string = "blue - sky";
$string = preg_replace("/[^A-Za-z0-9 ]/", '-', $string);
echo $string;
result - blue---sky
expected - blue-sky.
use the + sign to replace more than one character with one replacement character:
string = preg_replace("/[^A-Za-z0-9]+/", '-', $string);
I always have problems with strings full of weird characters that are not white spaces but they do count as an element of the string. How can I remove all this characters from the string (not removing inner spaces)?
I am using preg_replace, but it eliminates inner spaces which I want to keep.
$string = preg_replace('/\s+/', '', $string);
Php shows that "My string" has 40 elements
string(40)=>"
My string
"
And it should have only 9 just like that:
string(9)=>"My string"
This spaces at the beginning and end of the word are not feed, enter or tab since I've used string replace just like that:
str_replace("\r", "", $string);
str_replace("\t", "", $string);
str_replace(char(10), "", $string);
You can try the trim function
Example
$bad_text = "\t\tWhitespace is bad!!! ";
var_dump($bad_text);
$good_text = trim($bad_text);
var_dump($good_text);
I wrote this code to prepare a title for a link, but I think it is a bit bulky and am wondering if anyone with a better understanding of regex would be able to reduce the following function (by merging the relevant preg_replaces). I need it to strip all current hyphens, strip multiple spaces, ensure that it is solely alphanumeric apart from the space-replacing hyphen, replace all spaces with a single hyphen and ensure that the string doesn't start with a hyphen:
function prepareURLTitle($title)
{
return preg_replace("/\A-/", "", str_replace(" ", "-", preg_replace("/[^a-zA-Z0-9\s]/", "", preg_replace('/\s\s+/', ' ', preg_replace('/\s?-/', '', $title)))));
}
An example of input and it's output:
Input:
BRAND NEW - Gloves, 2 pack //Multiple spaces are in here but the blockquote won't allow me to display them
Output:
BRAND-NEW-Gloves-2-pack
trim(preg_replace('`[^a-z0-9]+`i','-',str_replace("'",'',$title)),'-')
I also replaced quotes with nothing, so strings like "The cat's meow" don't become "The-cat-s-meow".
function prepareURLTitle($title)
{
return preg_replace("[^A-Za-z0-9]+", "-", $title);
}
This should work. You need to replace multiple non-alphanumeric characters with a single "-".
preg_replace('~[^a-z\d]+~i','-',preg_replace('~^[^a-z\d]+(.*?)[^a-z\d]+$~i','$1',$title));
// or
preg_replace(array('~^[^a-z\d]+(.*?)[^a-z\d]+$~i','~[^a-z\d]+~i'),array('$1','-'),$title);
With an example…
$title = ' BRAND NEW - Gloves, 2 pack - ';
echo preg_replace(array('~^[^a-z\d]+(.*?)[^a-z\d]+$~i','~[^a-z\d]+~i'),array('$1','-'),$title);
will return
BRAND-NEW-Gloves-2-pack
function prepareURLTitle($title)
{
return preg_replace( "/[^a-zA-Z0-9]/", "-",str_replace("-", "", $title));
}
DEMO: http://codepad.org/lPSQQBys
OUTPUT:
BRAND-NEW--Gloves--2-pack