Stop PHP from adding backslashes to string [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a function that builds a regex based on an array. The problem is that PHP keeps adding backslashes to some of the characters, and it keeps messing up the regex.
Here is my function:
private static $allowedPermissions = [
/*SV*/
'user_add',
'user_edit',
'user_delete',
'user_view'];
$regexrule = '/';
foreach (self::$allowedPermissions as $allowedPermission) {
$regexrule .= '\b'.$allowedPermission.'\b';
if(end(self::$allowedPermissions) !== $allowedPermission) $regexrule .='|';
}
$regexrule .= "/";
return 'regex:'.$regexrule;
It is adding backslashes where I don't expect them:
regex:\/\\buser_add\\b|\\buser_edit\\b|\\buser_delete\\b|\\buser_view\\b|\\bpatient_add\\b|\\bpatient_edit\\b|\\bpatient_delete\\b|\\bpatient_view\\b|\\bmake_per\\b|\\bmake_per_withconfirmation\\b|\\bconfirm_per\\b|\\beval_per\\b|\\beval_per_withconfirmation\\b|\\bconfirm_per_report\\b\/
Backup screenshot of regex
Is there a workaround?

I found out that returning it in json format was adding the backslashes.

Related

Replace string with some condition in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this string
RRULE:FREQ=WEEKLY;COUNT=13;BYDAY=MO,SU
Now the part BYDAY=MO,SU can be in any position like
RRULE:FREQ=WEEKLY;BYDAY=WE,TH;INTERVAL=2;COUNT=5; -> BYDAY=WE
I just want to replace the value of BYDAY=value
let's say I have updated value of BYDAY=FR
I've tried to use str_replace() but the given value of of BYDAY can be anything like MO,TU,WE,TH
Using preg_replace, we can try:
$input = "RRULE:FREQ=WEEKLY;COUNT=13;BYDAY=MO,SU";
$output = preg_replace("/\bBYDAY=[^;]+/", "BYDAY=FR", $input);
echo $input . "\n" . $output;
This prints:
RRULE:FREQ=WEEKLY;COUNT=13;BYDAY=MO,SU
RRULE:FREQ=WEEKLY;COUNT=13;BYDAY=FR

Escaping regex in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have regexes stored in a txt file. How do I escape them in PHP? preg_quote doesn't help if I use the output in an array, throws fatal error. (The following each are on new lines in the txt file)
/[^a-z\/'"]eval\([^\)]+['"\s\);]+/i
/\$auth_pass\s*=.+;/i
/document\.write\((['"])<iframe .+<\/iframe>\1\);*/i
/preg_replace\s*\(.+[\/\#\|][i]*e[i]*['"].+\)/i
/<\?.+?exec\(.+?system\(.+?passthru\(.+fwrite\(.+/s
/RewriteRule [^ ]+ http\:\/\/(?!127\.).*/i
/<\?[\shp]*\#?error_reporting\(0\);.+?[a-z0-9\/\-\='"\.]{2000}.*?($|\?>)/i
/\<a [^\>]+\>\<span style="color\:\#F1EFE4;"\>(.+?)\<\/span\>\<\/a\>\<span style="color\:\#F1EFE4;"\>(.+?)\<\/span\>/i
/(<!\d)\$[\$\{]*[a-z\-\_0-9]+[\} \t]*(\[[^\]]+\][ \t]*)*\(.*?\)\;/i
/\#(\w+)\#.+?\#\/\1\#/is
/(\$[a-z_0-9]+[=\s\#]+)?create_function\([^,]+,[\s\$\.\[\]a-z_0-9]+[\s\)]+;*/i
/json2\.min\.js/i
/(RewriteCond \%\{HTTP_USER_AGENT\} .+\s+)+RewriteRule \^.*\$ http:\/\/(?!127\.).*/i
/<title>[^<]*hack[3e][rd]/i
I think you need to adjust your call to preg_quote(). Something like this:
Preg_match("|" . preg_quote($str, "|") . "|", $content->content)
See another post with a similar question:
PHP String to Regex

How to make a php crawler to search particular string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I create a crawler in php which search for a particular string in a webpage and returns whether it is present or not?
Try this function I made, it takes in the pages URL and the string to look for in this URL's content.
<?php
var_dump(searchPage("http://google.com", "Tacos")); //False
var_dump(searchPage("http://google.com", "Google")); //True
function searchPage($url, $string){
$input = file_get_contents($url);
if (strpos($input,$string) !== false) {
return true;
}else{
return false;
}
}
?>
I hope this helped,
Sebastian

delete Persian's Chars of string in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In PHP, i have strings , similar:
"www.mysite.com/fa/doc/report/67571/مطذح کردنو تت";
"www.mysite.com/fa/571/نهتال اهخع";
"www.mysite.com/fa/";
I want if there are Persian's Chars of string, delete them.
Output:
www.mysite.com/fa/doc/report/67571/
www.mysite.com/fa/571/
www.mysite.com/fa/
How can i do this?
You should try this code.
<?php
echo remove_persian("www.mysite.com/fa/doc/report/67571/مطذح کردنو تت");
function remove_persian($text)
{
return preg_replace('#[^a-zA-Z0-9./]#', '', $text);
}
This will output like this:
www.mysite.com/fa/doc/report/67571/
You can use a regex :
$clean = preg_replace('[^a-zA-Z\/\d\s:]', '', $url);

Encoding equals symbol to html equivalent php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I attempted encoding a '=' symbol to its html equivalent through the use of:
htmlentities("This is my test and it = this");
The result is:
<p>This is my test and it = this</p>1
Notice how the equals sign is not encoded? I know there is a HTML equivalent.
What is an alternative function I can use to encode this string?
Thanks.
I know there is a HTML equivalent
The equals sign isn't encoded for HTML, there is no reason to do so.
You might be thinking of URL-encoding, which would be %3d:
urlencode("This is my test and it = this");
// => "This+is+my+test+and+it+%3D+this"
There's no need to encode the =; it's HTML-safe. If you really want to, though: =
echo str_replace('=', '=', htmlentities("This is my test and it = this"));
Demo

Categories