php function preg_quote escapes these characters . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
Could you please suggest what I could use to escape these characters: + - && || ! ( ) { } [ ] ^ " ~ * ? : /
Edited (as requested by "on hold"):
I'm implementing a Solr search and it says:
Solr gives the following characters special meaning when they appear
in a query:
+ - && || ! ( ) { } [ ] ^ " ~ * ? : /
To make Solr interpret any of these characters literally, rather as a special character, precede the
character with a backslash character .
This may help you:
<?php
$subject = "+ - && || ! ( ) { } [ ] ^ \" ~ * ? : /";
$result = preg_replace('%([+\-&|!(){}[\]^"~*?:/]+)%', '\\\\$1', $subject);
echo $result;
?>
http://ideone.com/EYV1ID
Maybe not the best approach but will work:
str_replace('+','\+',$MyString);
str_replace('-','\-',$MyString);
...............................;
...............................;
Working function
function escapeWildcards($s){
$escapeChars = [ '%', '_'];
foreach ($escapeChars as $escapeChar){
$s = str_replace($escapeChar, '\\'.$escapeChar, $s);
}
return $s;
}
Related
What I am trying to do is create a syntax highlighter.What I need help with is creating reg expression to find and highlight punction marks but I want the match to exclude strings between certain characters "",'' and also exclude strings that have been commented or defined//,/**/,#. I am currently useing this pattern
'/(!|%|\^|\*|\(|\)|\+|\=|\-|>|<|\?|,|\.|\:|\;|\'|&|\[|\]|\}|\{|~)(?=[^>]*(<|$))/'
This part seems to work to exclude stuff between tags but honestly it looks incorrect and I am clueless on how to structure the pattern correctly.
(?=[^>]*(<|$))
Here is my Highlight class
class SyntaxHighlight
{
public static function process($s,$lang,$raw = false)
{
$orig_s = $s;
$regexp = array(
//Keywords working
'/(?<!\w|\$|\%|\#|>)(and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else|
elseif|new|delete|try|throw|catch|finally|class|function|string|
array|object|resource|var|bool|boolean|int|integer|float|double|
real|string|array|global|const|static|public|private|protected|
published|extends|switch|true|false|null|void|this|self|struct|
char|signed|unsigned|short|long|break|goto|static_cast|const_cast|
LIMIT|ASC|DESC|ORDER|BY|SELECT|FROM|WHERE)(?!\w|=")/iux'
=> '<span class="K">$1</span>',
//$var, %var, #var
'/(?<!\w)(
(\$|\%|\#)(\->|\w)+
)(?!\w)/ix'
=> '<span class="V">$1</span>',
// Numbers (also look for Hex)
'/(?<!\w)(
0x[\da-f]+|
\d+
)(?!\w)(?=[^"]*(<|"))/ix'
=> '<span class="N">$1</span>',
//comments
'/(\/\*.*?\*\/|
\/\/.*?\n|
\#.[^a-zA-Z0-9]+?\|
\<\!\-\-[\s\S]+\-\-\>|
(?<!\\\)\'(.*?)(?<!\\\)\'
)/isx'
=> '<span class="C">$1</span>',
//char strings
'/\'(.+?)\'(?=[^>]*(<|$))/'
=> "<span class='CS'>'$1'</span>",
//back quote strings
'/\`(.+?)\`(?=[^>]*(<|$))/'
=> "<span class='BS'>`$1`</span>",
//strings
'/"(.+?)"(?=[^>]*(<|$))/'
=> '<span class="S">"$1"</span>',
//Punc
'/(!|%|\^|\*|\(|\)|\+|\=|\-|>|<|\?|,|\.|\:|\;|\'|&|\[|\]|\}|\{|~)(?=[^>]*(<|$))/'
=> '<span class="P">$1</span>',
);
$s = preg_replace( array_keys($regexp), array_values($regexp), $s);
if($lang == "Text" || $lang == "Other")
$s = $orig_s;
if($raw == "false")
$s = self::DisplayLineCount($s);
return '<pre style = "background-color:white;margin:0auto;text-align:left;overflow:auto;">'.$s.'</pre>';
}
private static function DisplayLineCount($s)
{
$i=1;
$count = substr_count($s, "\n");
$s = "<span style = 'color:#dfecf2;'>".$i."\t</span>".$s;
while($i <= $count)
{
++$i;
$s = preg_replace("/[\n]/","<span style = 'color:#dfecf2;'>".$i."\t</span>",$s,1);
}
return $s;
}
}
I am getting the syntax to highligh from a database and calling the function like this.
if(isset($row['syntax']) && $row['syntax'] == $cat_ && isset($row['title']) && $row['title'] == $top_ && isset($row['id']) && $row['id'] == $id_)
{
$id_ = isset($row['id']) ? $row['id'] : '';
$top_ = isset($row['title']) ? $row['title'] : '';
$top_ = htmlspecialchars($top_);
$auth_ = isset($row['author']) ? $row['author'] : '';
$date_ = isset($row['date']) ? $row['date'] : '';
$post_ = isset($row['paste']) ? $row['paste'] : '';
$newtext = htmlspecialchars($post_);
echo SyntaxHighlight::process($newtext,$cat_,$raw);
}
I am very new to useing reg expression patterns so please forgive me and thank you in advance.
I'm having an issue with my register page, i noticed that people can register with alt codes like this "ªµµª" and i tried to fix it by using preg_replace but when i did that i couldn't register anymore, atleast not with the worldwide alphabet
final public function validName($username)
{
if(strlen($username) <= 25 && ctype_alnum($username))
{
return true;
}
return false;
}
Tried to fix it by replacing it with this
if(strlen($username) <= 25 && preg_match("/[^a-zA-Z0-9]+/", $username))
But i'm obviously doing something wrong...
Apparently, you are confusing two different uses of the caret (^) metacharacter.
Indeed, it may be two things in a regular expression:
It may assert the start of the subject, which is what you probably want.
It may negate the class, which is what you're doing in your code.
Source: http://php.net/manual/en/regexp.reference.meta.php
Here is a modified version of your code, with the caret (^) and dollar ($) signs to assert the start and the end of the strings you're analyzing:
function validName($username)
{
if (strlen($username) <= 25 && preg_match("/^[a-zA-Z0-9]+$/", $username))
{
return true;
}
return false;
}
$names = array(
'Abc1',
'Abc$',
"ªµµª"
);
foreach ($names as $name) {
echo "<br>" . $name . ': ' . (validName($name) ? 'valid' : 'invalid');
}
// -- Returns:
// Abc1: valid
// Abc$: invalid
// ªµµª: invalid
Note that you may reduce the code inside your function to one line:
function validName($username)
{
return strlen($username) <= 25 && preg_match("/^[a-zA-Z0-9]+$/", $username);
}
Please see the following code. I want to remove all the unauthorized characters such as . / \ | !##$%^&*() _ - = + ~ < > , ? : ; " ' [] { } and the ` character and and all the empty spaces input.
I want receive only English characters and Numbers allowed.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_input($_POST["user"]);
$password = test_input($_POST["pass"]);}
How should be the test_input() function?
function test_input($string){
if(preg_match('/[^a-z_\-0-9]/i', $string))
{
echo "not valid string";
}
}
I have the following code that creates the meta-description. I'm getting a 0 as meta-description when trying to add a text after the $info['desc'].
The original code is
function apply_meta($info) {
if (isset($info['desc']) && !empty($info['desc'])) {
define('META_DESC', $info['desc']);
}
What I did is :
function apply_meta($info) {
if (isset($info['desc']) && !empty($info['desc'])) {
define('META_DESC', $info['desc'] + 'my text. Read more about ' + $info['desc'] );
}
Period (.) is used in PHP to concat the strings. Replace your + sign with period like this:
function apply_meta($info) {
if (isset($info['desc']) && !empty($info['desc'])) {
define('META_DESC', $info['desc'] . 'my text. Read more about ' . $info['desc'] );
}
PHP doesn't use + to concatenate strings, it uses a period. Replace your plus signs with periods and it should work.
I want to find the most lightweight solution to validate a string as a letter or number + ?. Eg: a? or 1? etc.
if (preg_match('/^[a-z0-9]\?$/', $str)) {
// yup, it's a letter or number + ?
}
slighty faster than regular expression is function:
// return true or false
function validate($str) {
$str0 = ord($str[0]);
return(
(
($str0 >= 97 && $str0 <= 122) or
($str0 >= 48 && $str0 <= 57)
) &&
(
$str[1] == '?'
)
);
}
Some time ago, i've written a lightweight-validation class. Maybe you can use it.
For example:
$oValidator = new Validator();
$oValidator->isValid('a', 'alpha_numeric|max_length[1]'); //true
$oValidator->isValid('1', 'alpha_numeric|max_length[1]'); //true
$oValidator->isValid('ab', 'alpha_numeric|max_length[1]'); //false
$oValidator->isValid('1337', 'alpha_numeric|max_length[1]'); //false
Example:
http://sklueh.de/2012/09/lightweight-validator-in-php/
github:
https://github.com/sklueh/Lightweight-PHP-Validator
OK This is the fastest way
$allowed_char = Array();
for($i=ord('a');$i<=ord('z');$i++) $allowed_char[chr($i)] = true;
for($i=ord('0');$i<=ord('9');$i++) $allowed_char[chr($i)] = true;
function validate($str) {
global $allowed_char;
return $allowed_char[$str[0]] && $str[1] == '?' && !isset($str[2]);
}
Regexp = 2.0147299766541s
This solution = 1.6041090488434s
So it's 20% faster than Regexp solution :)