Strip bad Windows filename characters - php

I found this function that tests whether a string is Windows filename and folder friendly:
function is_valid_filename($name) {
$parts=preg_split("/(\/|".preg_quote("\\").")/",$name);
if (preg_match("/[a-z]:/i",$parts[0])) {
unset($parts[0]);
}
foreach ($parts as $part) {
print "part = '$part'<br>";
if (preg_match("/[".preg_quote("^|?*<\":>","/")."\a\b\c\e\x\v\s]/",$part)||preg_match("/^(PRN|CON|AUX|CLOCK$|NUL|COMd|LPTd)$/im",str_replace(".","\n",$part))) {
return false;
}
}
return true;
}
What I'd rather have is a function that strips all the bad stuff from the string. I tried to basically replace preg_match with preg_replace but no cigar.

Following Gordon's reference, this gives:
$bad = array_merge(
array_map('chr', range(0,31)),
array("<", ">", ":", '"', "/", "\\", "|", "?", "*"));
$result = str_replace($bad, "", $filename);

Related

remove - and make every letter after as capital in the string - PHP [duplicate]

I want to take a string like this: 'this-is-a-string' and convert it to this: 'thisIsAString':
function dashesToCamelCase($string, $capitalizeFirstCharacter = false) {
// Do stuff
return $string;
}
I need to convert "kebab-case" to "camelCase".
No regex or callbacks necessary. Almost all the work can be done with ucwords:
function dashesToCamelCase($string, $capitalizeFirstCharacter = false)
{
$str = str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
if (!$capitalizeFirstCharacter) {
$str[0] = strtolower($str[0]);
}
return $str;
}
echo dashesToCamelCase('this-is-a-string');
If you're using PHP >= 5.3, you can use lcfirst instead of strtolower.
Update
A second parameter was added to ucwords in PHP 5.4.32/5.5.16 which means we don't need to first change the dashes to spaces (thanks to Lars Ebert and PeterM for pointing this out). Here is the updated code:
function dashesToCamelCase($string, $capitalizeFirstCharacter = false)
{
$str = str_replace('-', '', ucwords($string, '-'));
if (!$capitalizeFirstCharacter) {
$str = lcfirst($str);
}
return $str;
}
echo dashesToCamelCase('this-is-a-string');
This can be done very simply, by using ucwords which accepts delimiter as param:
function camelize($input, $separator = '_')
{
return str_replace($separator, '', ucwords($input, $separator));
}
NOTE: Need php at least 5.4.32, 5.5.16
Overloaded one-liner, with doc block...
/**
* Convert underscore_strings to camelCase (medial capitals).
*
* #param {string} $str
*
* #return {string}
*/
function snakeToCamel ($str) {
// Remove underscores, capitalize words, squash, lowercase first.
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
}
In Laravel use Str::camel()
use Illuminate\Support\Str;
$converted = Str::camel('foo_bar');
// fooBar
this is my variation on how to deal with it. Here I have two functions, first one camelCase turns anything into a camelCase and it wont mess if variable already contains cameCase. Second uncamelCase turns camelCase into underscore (great feature when dealing with database keys).
function camelCase($str) {
$i = array("-","_");
$str = preg_replace('/([a-z])([A-Z])/', "\\1 \\2", $str);
$str = preg_replace('#[^a-zA-Z0-9\-_ ]+#', '', $str);
$str = str_replace($i, ' ', $str);
$str = str_replace(' ', '', ucwords(strtolower($str)));
$str = strtolower(substr($str,0,1)).substr($str,1);
return $str;
}
function uncamelCase($str) {
$str = preg_replace('/([a-z])([A-Z])/', "\\1_\\2", $str);
$str = strtolower($str);
return $str;
}
lets test both:
$camel = camelCase("James_LIKES-camelCase");
$uncamel = uncamelCase($camel);
echo $camel." ".$uncamel;
I would probably use preg_replace_callback(), like this:
function dashesToCamelCase($string, $capitalizeFirstCharacter = false) {
return preg_replace_callback("/-[a-zA-Z]/", 'removeDashAndCapitalize', $string);
}
function removeDashAndCapitalize($matches) {
return strtoupper($matches[0][1]);
}
You're looking for preg_replace_callback, you can use it like this :
$camelCase = preg_replace_callback('/-(.?)/', function($matches) {
return ucfirst($matches[1]);
}, $dashes);
here is very very easy solution in one line code
$string='this-is-a-string' ;
echo str_replace('-', '', ucwords($string, "-"));
output ThisIsAString
Try this:
$var='snake_case';
$ucword= ucword($var,'_');
echo $ucword;
Output:
Snake_Case
remove _ with str_replace
str_replace('_','',$ucword); //SnakeCase
and result
$result='SnakeCase'; //pascal case
echo lcfirst('SnakeCase'); //snakeCase (camel case)
the important thing is the approach here I used snake case and camel case in the example
$string = explode( "-", $string );
$first = true;
foreach( $string as &$v ) {
if( $first ) {
$first = false;
continue;
}
$v = ucfirst( $v );
}
return implode( "", $string );
Untested code. Check the PHP docs for the functions im-/explode and ucfirst.
One liner, PHP >= 5.3:
$camelCase = lcfirst(join(array_map('ucfirst', explode('-', $url))));
The TurboCommons library contains a general purpose formatCase() method inside the StringUtils class, which lets you convert a string to lots of common case formats, like CamelCase, UpperCamelCase, LowerCamelCase, snake_case, Title Case, and many more.
https://github.com/edertone/TurboCommons
To use it, import the phar file to your project and:
use org\turbocommons\src\main\php\utils\StringUtils;
echo StringUtils::formatCase('sNake_Case', StringUtils::FORMAT_CAMEL_CASE);
// will output 'sNakeCase'
Here's the link to the method source code:
https://github.com/edertone/TurboCommons/blob/b2e015cf89c8dbe372a5f5515e7d9763f45eba76/TurboCommons-Php/src/main/php/utils/StringUtils.php#L653
function camelize($input, $separator = '_')
{
return lcfirst(str_replace($separator, '', ucwords($input, $separator)));
}
echo ($this->camelize('someWeir-d-string'));
// output: 'someWeirdString';
Try this ;)
$string = 'this-is-a-string';
$separator = '-';
$stringCamelize = str_replace(
$separator,
'',
lcfirst(
ucwords(
strtolower($string),
$separator
)
)
);
var_dump($stringCamelize); // -> 'thisIsAString'
Alternatively, if you prefer not to deal with regex, and want to avoid explicit loops:
// $key = 'some-text', after transformation someText
$key = lcfirst(implode('', array_map(function ($key) {
return ucfirst($key);
}, explode('-', $key))));
Another simple approach:
$nasty = [' ', '-', '"', "'"]; // array of nasty characted to be removed
$cameled = lcfirst(str_replace($nasty, '', ucwords($string)));
Many good solutions above, and I can provide a different way that no one mention before. This example uses array. I use this method on my project Shieldon Firewall.
/**
* Covert string with dashes into camel-case string.
*
* #param string $string A string with dashes.
*
* #return string
*/
function getCamelCase(string $string = '')
{
$str = explode('-', $string);
$str = implode('', array_map(function($word) {
return ucwords($word);
}, $str));
return $str;
}
Test it:
echo getCamelCase('This-is-example');
Result:
ThisIsExample
Some very good solutions here. I compiled them together for easy c&p
declare(strict_types=1);
/**
* convert kebab-case to PascalCase
*/
function kebabToPascal( string $str ): string {
return str_replace( ' ', '', ucwords( str_replace( '-', ' ', $str ) ) );
}
/**
* convert snake_case to PascalCase
*/
function snakeToPascal( string $str ): string {
return str_replace (' ', '', ucwords( str_replace( '_', ' ', $str ) ) );
}
/**
* convert snake_case to camelCase
*/
function snakeToCamel( string $str ): string {
return lcfirst( snakeToPascal( $str ) );
}
/**
* convert kebab-case to camelCase
*/
function kebabToCamel( string $str): string {
return lcfirst( kebabToPascal( $str ) );
}
echo snakeToCamel( 'snake_case' ). '<br>';
echo kebabToCamel( 'kebab-case' ). '<br>';
echo snakeToPascal( 'snake_case' ). '<br>';
echo kebabToPascal( 'kebab-case' ). '<br>';
echo kebabToPascal( 'It will BREAK on things-like_this' ). '<br>';
In Yii2 you can use yii\helpers\Inflector::camelize():
use yii\helpers\Inflector;
echo Inflector::camelize("send_email");
// outputs: SendEmail
Yii provides a lot of similar functions, see the Yii2 Docs.
function camelCase($text) {
return array_reduce(
explode('-', strtolower($text)),
function ($carry, $value) {
$carry .= ucfirst($value);
return $carry;
},
'');
}
Obviously, if another delimiter than '-', e.g. '_', is to be matched too, then this won't work, then a preg_replace could convert all (consecutive) delimiters to '-' in $text first...
If you use Laravel framework, you can use just camel_case() method.
camel_case('this-is-a-string') // 'thisIsAString'
Here is another option:
private function camelcase($input, $separator = '-')
{
$array = explode($separator, $input);
$parts = array_map('ucwords', $array);
return implode('', $parts);
}
$stringWithDash = 'Pending-Seller-Confirmation';
$camelize = str_replace('-', '', ucwords($stringWithDash, '-'));
echo $camelize;
output: PendingSellerConfirmation
ucwords second(optional) parameter helps in identify a separator to camelize the string.
str_replace is used to finalize the output by removing the separator.
Here is a small helper function using a functional array_reduce approach.
Requires at least PHP 7.0
private function toCamelCase(string $stringToTransform, string $delimiter = '_'): string
{
return array_reduce(
explode($delimiter, $stringToTransform),
function ($carry, string $part): string {
return $carry === null ? $part: $carry . ucfirst($part);
}
);
}
private function dashesToCamelCase($string)
{
$explode = explode('-', $string);
$return = '';
foreach ($explode as $item) $return .= ucfirst($item);
return lcfirst($return);
}
This works for me:
function camelCase($string){
$chunks = preg_split("/\s+|-|_/",$string);
$camel = "";
foreach ($chunks as $idx => $chunk){
if ($idx===0){
$camel = strtolower($chunk);
}else{
$camel .= ucfirst($chunk);
}
}
return $camel;
}
The shortest and most elegant solution would be:
function dashesToCamelCase($string, $capitalizeFirstCharacter = false) {
$result = join("", array_map("ucfirst", explode("-", $string)));
if ($capitalizeFirstCharacter === false) {
return lcfirst($result);
}
return $result;
}
One Line Option
echo str_replace(' ', '', lcfirst(ucwords(str_replace("_", " ", 'facebook_catalog_id_type'))));
//output: facebookCatalogIdType
Try this:
return preg_replace("/\-(.)/e", "strtoupper('\\1')", $string);
This is simpler :
$string = preg_replace( '/-(.?)/e',"strtoupper('$1')", strtolower( $string ) );

Add hyperlinks to words in php

I have found this function on buildinternet.com.
function tag_it($text) {
$text = preg_replace("/:(\w+):/", '$1',$text);
return $text;
}
What it does is adding hyperlinks to words enclosed in ":" and the problem is that it works only with single words and it doesn`t work with words that has a single quote.
So if I do this,
$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:.";
echo tag_it($test);
only to the :word1: will be added a hyperlink, the rest will be ignored.
I don`t know too much php and I would appreciate if someone could make the function work with more than one word and with a single quote too.
Thank you!
Edit:
So I have tried the following to add a different link to words enclosed in "#"
function tag_it($text) {
$out = preg_replace_callback(
"/:([^\:]+):/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return ''.$linkText.'';
},
preg_replace_callback(
"/#([^\#]+)#/",
function($m1) {
$linkText1 = $m1[1];
$link1 = str_replace('"', '', $m1[1]);
$link1 = str_replace("'", "", $m1[1]);
$link1 = str_replace(" ", "_", $link1);
return ''.$linkText1.'';
},
$text
)
);
return $out;
}
$test = "one :word:, #two words#, :this is a phrase:, this has a single quote :don:'t or :don't forget:.";
echo tag_it($test);
and i get this
one word, two_words,_/" title="//www.example.com/blog/two_words/" title="two words" target="_blank">two words, " target="_blank">//www.example.com/blog/two_words/" title="two words" target="_blank">two words, this is a phrase, this has a single quote don't or don't forget:.
It seems to work for the 1st word enclosed in ":" and is trying to work with the words enclosed in "#" too but something is happening along the way and I can`t figure it out.
Any tip is really appreciated.
Thank you!
Change the crucial line in the function to the following:
$text = preg_replace("/:([^:]+):/", '$1', $text);
change this /:(\w+):/ to this /:([^:]+):/
Try this hope this simple one will help you out..
Regex: :([^\:]+):
:([^\:]+): it will match : then all till not : and then : in end
Try this code snippet here
<?php
ini_set('display_errors', 1);
$test = "one :word1:, :two words:, :this is a phrase:, this has a single quote :don't: or :don't forget:.";
echo tag_it($test);
function tag_it($text)
{
$text = preg_replace("/:([^\:]+):/", '$1', $text);
return $text;
}
I'll let you finish it off, above are all valid, but leave you with broken links, so building on them, I'd do something like:
function tag_it($text) {
$out = preg_replace_callback(
"/:([^:]+):/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', "", $m[1]);
$link = str_replace("'", "", $m[1]);
return ''.$linkText.'';
},
$text);
return $out;
}
You'll need to finish it off to replace spaces with - or _ or something but should be easy enough to work it out.
To answer to my "Edit:", I have figured out the problem.
Here is the full function
function tag_it($text) {
$out = preg_replace_callback(
"/:([^\:]+):/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return ''.$linkText.'';
},$text
);
$out = preg_replace_callback(
"/#([^\#]+)#/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return ''.$linkText.'';
},$out
);
$out = preg_replace_callback(
"/#([^\#]+)#/",
function($m) {
$linkText = $m[1];
$link = str_replace('"', '', $m[1]);
$link = str_replace("'", "", $m[1]);
$link = str_replace(" ", "_", $link);
return ''.$linkText.'';
},$out
);
return $out;
}
$test = "one :word:, #two words#, #this is a phrase#, this has a single quote #don't# or :don't forget:.";
echo tag_it($test);

preg_replace remove all unwanted characters

i want to block or remove all unwanted characters from from my site
characters like ᾄҭᾄ
or нєℓℓσ
ĤĔĹĹŐ
etc..
my code now is
class badWordsC
{
public function check($text)
{
$badwords = 'com|net|org|info|.name|.biz|.me|.tv|.tel|.mobi|.asia|.uk|.eu|.us|.in|.tk|.cc|.ws|.bz|.mn|.co|.tw|.vn|.es|.pw|.club|.ca|.cn|.email|.photography|.photos|.tips|.solutions|.center|.gallery|.kitchen|.land|.technology|.today|.academy|.computer|.shoes|.careers|.domains|.coffee|.link|.guru|.estate|.company|.bike|.clothing|.holdings|.plumbing|.singles|.ventures|.camera|.equipment|.graphics|.lighting|.construction|.contractors|.directory|.diamonds|.enterprises|.voyage|.recipes|.gift|.site|.ly|.gq|.cf|.ga|.ml|.tk|in|rb2';
$badwords .= 'type|ingoogle';
$badwords = explode('|', $badwords);
$goodwords = 'youtube.com|prntscr.com|az545221.vo.msecnd.net';
$goodwords .= 'wink|crying|fingerscrossed|blushing|wondering|inlove|evilgrin|yawning|puking|in';
$goodwords = explode('|', $goodwords);
$text = str_replace($goodwords, '', $text);
$text = trim(preg_replace('/\s\s+/', '', $text));
$text = preg_replace('/\P{L}+/u', '', $text);
foreach ($badwords as $word)
{
if (strpos($text, $word) !== false || strpos($text, strtoupper($word)) !== false)
{
return false;
}
}
$text = preg_replace("/[a-zA-Z0-9]/", '', $text);
$text = preg_replace(array('/)/','/(/','/;/','/-/','/+/','/لأ/','/لإ/','/لا/','/إ/','/أ/', '/ا/', '/ض/', '/ص/', '/ث/', '/ق/', '/ف/', '/غ/', '/ع/', '/ه/', '/خ/', '/ح/', '/ج/', '/د/', '/ش/', '/س/', '/ي/', '/ب/', '/ل/', '/ت/', '/ن/', '/م/', '/ك/', '/ط/', '/ئ/', '/ء/', '/ؤ/', '/ر/', '/ى/', '/ة/', '/و/', '/ز/', '/ظ/', '/ذ/', '/ـ/'), '', $text);
if($text != '')
{
return false;
}
return true;
}
}
its working but not bloking or removing characters like н Ĕ Ő
any idea ?
The u modifier you will need to use you also need to expand your character class to include the non-ascii characters.
I'd use:
/[[:alnum:]]/u
Regex Demo: https://regex101.com/r/iS1yZ2/2
That is a posix bracket, you can see more of those here, www.regular-expressions.info/posixbrackets.html.
Also in your second expression the + needs to be escaped (or put in a character class, there are some symbols putting in a character won't fix -, ], ^) because that is a quantifier. There is a PHP function that will escape special characters, preg_quote.

preg_replace is modifying JavaScript

I have a template tool, that replaces placeholders one of the pieces of the tool loads other files, here is what I am using for debugging:
var_dump($string);
$tmp = preg_replace('/\\$import\(("|\')' . $f . '("|\')\).*;/i', $string, $tmp);
var_dump($tmp);
The first var_dump prints out the contents of a file, and in the file there is this line of JavaScript:
$("#image-menu .info").html(text.replace(/(.+?:)/, "<b>$1</b>"));
After the pre_replace I have the second var_dump which then prints out this:
$("#image-menu .info").html(text.replace(/(.+?:)/, "<b>"</b>"));
As you can see $1 was replaced by a ", and I am not sure why. Any ideas as to why it is getting replaced?
Here is the full method:
private function loadIncludes(){
$tmp = $this->template;
$matches = array();
preg_match_all('/(\\$import\(("|\')(.+?)("|\')\).*;)/i', $tmp, $matches);
$files = $matches[3];
$replace = 0;
foreach($files as $key => $file){
$command = preg_replace("/\\\$import\((\"|').+?(\"|')\)/", "", $matches[0][$key]);
$string = $this->import($file);
$string = $this->runFunctions($string, "blah" . $command);
$f = preg_quote($file, "/");
var_dump($string);
$tmp = preg_replace('/\\$import\(("|\')' . $f . '("|\')\).*;/i', $string, $tmp);
var_dump($tmp);
$replace++;
}
$this->template = $tmp;
if($replace > 0){
$this->loadIncludes();
}
}
Within single quotes you can't use control characters like \r or \n, meaning you don't have to double-escape your $. Your \\$ can simply be \$.

PHP: explode but ignore escaped delimiter

I have a flatfile database and it is data seperated by delimiters.
I allow people to use the delimiter in their input but I make sure to escape it with a \ beforehand.
The problem is my explode() function still attempts to split the escaped delimiters, so how do I tell it to ignore them?
Use preg_split instead. By using a regex you can match a delimeter only if it is not preceded with a backslash.
Edit:
preg_split('~(?<!\\\)' . preg_quote($delimeter, '~') . '~', $text);
None of the solutions here correctly handle any number of escape characters, or they leave them in the output. Here's an alternative:
function separate($string, $separator = '|', $escape = '\\') {
if (strlen($separator) != 1 || strlen($escape) != 1) {
trigger_error(__FUNCTION__ . ' requires delimiters to be single characters.', E_USER_WARNING);
return;
}
$segments = [];
$string = (string) $string;
do {
$segment = '';
do {
$segment_length = strcspn($string, "$separator$escape");
if ($segment_length) {
$segment .= substr($string, 0, $segment_length);
}
if (strlen($string) <= $segment_length) {
$string = null;
break;
}
if ($escaped = $string[$segment_length] == $escape) {
$segment .= (string) substr($string, ++$segment_length, 1);
}
$string = (string) substr($string, ++$segment_length);
} while ($escaped);
$segments[] = $segment;
} while ($string !== null);
return $segments;
}
This will process a raw string like foo\|ba\r\\|baz| into foo|bar\, baz, and an empty string.
If you want to retain the escape character in the output, you will have to modify the function.
Note: this will have unpredictable behaviour if you're using mb function overloading.
Input Data
key1=val1;key2=val2start\;val2end;key3=val3\\;key4=val4\\\;key5=val5\\\\;key6=val6
REGEX
/(.*?[^\\](\\\\)*?);/
Example
<?php
$data="key1=val1;key2=val2start\\;val2end;key3=val3\\\\;key4=val4\\\\\\;key5=val5\\\\\\\\;key6=val6";
$regex='/(.*?[^\\\\](\\\\\\\\)*?);/';
preg_match_all($regex, $data.';', $matches);
print_r($matches[1]);
Output
Array
(
[0] => key1=val1
[1] => key2=val2start\;val2end
[2] => key3=val3\\
[3] => key4=val4\\\;key5=val5\\\\
[4] => key6=val6
)
You will find this solution more useful than using regex for large strings. I employ a stream to allow usage of fgetcsv, which is optimized for this sort of thing.
<?php
function escaped_explode($string,$delimit,$escape=NULL,$enclosure=NULL,$max_line_length=0){
$r=[];
$stream = fopen('php://memory','r+');
fwrite($stream, $string);
rewind($stream);
while (($data = fgetcsv($stream,$max_line_length,$delimit,$enclosure,$escape)) !== FALSE)
$r=array_merge($r,$data);
fclose($stream);
return $r;
}
?>
Usage:
$pipelined_values = escaped_explode($source,'|','\\');
This is convenient also because you have the option of using enclosures, such as quotes, instead of only escape characters. This is nice if you run into parsing someone's blobs of JSON values, or other syntax, as you can both enclose and escape.
$source= <<<JSON
'{ "key":"val", "n":0}',
'{ "key":"val", "n":1, "name": "French du\'Name" }',
'{ "key":"val", "n":2}'
JSON;
Can be interpreted
<?php
$objects=[];
$raw= escaped_explode($source, ',', '\\', "'");
foreach($raw as $r)
$objects[] = json_decode($r);
?>

Categories