preg-match

Is it safe to use user's RegEx?

限于喜欢 提交于 2019-12-04 00:49:13
I want to add a feature to my website to let users search the texts with RegEx . But, is it safe to let the users do something like that ? preg_match('/' . $user_input_regex . '/', $subject); There is a possible attack on this code called a ReDoS attack (Regular expression Denial of Service). The Regular expression Denial of Service (ReDoS) is a Denial of Service attack, that exploits the fact that most Regular Expression implementations may reach extreme situations that cause them to work very slowly (exponentially related to input size). An attacker can then cause a program using a Regular

preg_replace change link from href

老子叫甜甜 提交于 2019-12-03 21:41:47
I need to replace urls in the page taken by curl and add correct link to images and links. My php curl code is: <?php $result = '<a href="http://host.org"><img src="./sec.png"></a> <link href="./styles.css" rel="alternate stylesheet" type="text/css" /> <script type="text/javascript" src="./style.js"></script>'; echo $result; if (!preg_match('/src="https?:\/\/"/', $result)) { $result = preg_replace('/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', "src=\"http://google.com/\\3\"", $result); } echo $result; if (!preg_match('/href="https?:\/\/"/', $result)) { $result = preg_replace('/href="(http:\/\/([^\/]

Logical operator AND with php regular expression

此生再无相见时 提交于 2019-12-03 13:31:36
问题 I'd like to use a kind of logical operator "AND" in my regular expression. I tried this: (?=exp1)(?=exp2) But in PHP ?= doesn't work and need to write my program in PHP language. Is there another method? The expression has to match if there are present all the conditions and in any order. I don't wanna write every permutation like: (exp1)(exp2)(exp3)|(exp1)(exp3)(exp2)|.... 回答1: PHP does support lookahead expressions. You're probably not using them correctly, though. Assuming you want to

Check If Preg Match False instead of True

风流意气都作罢 提交于 2019-12-03 13:00:21
问题 I have this code that makes sure the username is only letters and numbers but then way my code is set up I need it to check if the preg_match is false. Right now it says "if secure echo this" I need it's logic to say "if not secure say this". Can someone help me out? if (preg_match('/[A-Z]+[a-z]+[0-9]+/', $username)) { echo 'Secure enough'; } 回答1: You can negate the condition like this: if (!preg_match('/^[A-Za-z0-9]+$/', $username)) { echo 'Secure enough'; } Also, your regex needs to be [A

preg_match: number-alphabets and commas only

荒凉一梦 提交于 2019-12-03 12:45:25
问题 How do I write a regular expression which matches number-alphabets and commas only? I came out with this one below but it doesnt work - it accepts other punctuation marks as well! # check for matches number-alphabets and commas only if(!preg_match('/([a-zA-Z0-9]|[a-zA-Z0-9\,])/', $cst_value)) { $error = true; echo '<error elementid="usr_username" message="'.$cst_name.' - please use number-alphabets and commas only."/>'; } Many thanks, Lau 回答1: You want: /^[a-zA-Z0-9,]+$/ You need the start ^

getting emails out of string - regex syntax + preg_match_all [closed]

狂风中的少年 提交于 2019-12-03 12:39:04
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . i'm trying to get emails out of a string. $string = "bla bla pickachu@domain.com MIME-Version: balbasur@domain.com bla bla bla"; $matches = array();

Get numbers from string using preg_match [closed]

喜夏-厌秋 提交于 2019-12-03 11:28:30
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . I have a string : <div id="post_message_957119941"> I want to fetch only the numbers (957119941) from this string using preg_match . This shouldn't be too hard. $str = '<div id="post_message_957119941">'; if ( preg_match ( '/post_message_([0-9]+)/', $str, $matches ) ) { print_r($matches); } Output: Array ( [0] => post_message

Maximum Hex value in regex

早过忘川 提交于 2019-12-03 09:53:33
Without using u flag the hex range that can be used is [\x{00}-\x{ff}] , but with u flag it goes up to a 4-byte value \x{7fffffff} ( [\x{00000000}-\x{7fffffff}] ). So if I execute the below code: preg_match("/[\x{00000000}-\x{80000000}]+/u", $str, $match); Will get this error : Warning: preg_match(): Compilation failed: character value in \x{...} sequence is too large So I can't match a letter like 𡃁 with equivalent hex value of f0 a1 83 81 . The question is not how to match these letters, but how this range & this boundary came from as u modifier should treat strings as UTF-16 PCRE supports

how can i use preg_match with alphanumeric and unicode acceptance?

只谈情不闲聊 提交于 2019-12-03 08:43:35
I am going to build a multilingual website with PHP and need to have a preg_match which passes all Unicode characters and numbers. i.e I need it to pass English letters, Spanish letters,Italian letters and as you may know I don't want to pass other characters like ' " _ - and ... I want some thing like this : $pattern='/^[unicode chars without \'-_;?]*$/u'; if(!preg_match($pattern, $url)) echo #error; Unicode property for letter is \pL so in preg_match : preg_match('/^\pL+$/u', $string); for an url you could add numbers \pN and dot: preg_match('/^[\pL\pN.]+/u', $string); 来源: https:/

php only allow letters, numbers, spaces and specific symbols using pregmatch

别说谁变了你拦得住时间么 提交于 2019-12-03 05:36:01
问题 on my php i use preg_match to validate input texts. if(preg_match('/^[a-zA-Z0-9]+$/', $firstname)) { } But this only allows alphanumeric and does not allow spaces. I want to allow spaces, alpha and numeric. and period(.) and dash(-) Please help me out here? thanks in advance. 回答1: Use preg_match('/^[a-z0-9 .\-]+$/i', $firstname) 回答2: If you not only want to allow ASCII, then use Unicode properties: preg_match('/^[\p{L}\p{N} .-]+$/', $firstname) \p{L} is any letter in any language, matches