Converting Perl Regex to PHP - php

I have the following Regex in PERL which I need to convert to PHP
if ($referrer_url =~ /\.$domain/) { }
I currently have the following in PHP to match it, but I'm not getting the same results:
if (preg_match("/\.$domain/", $referrer_url)) { }
Can anyone tell me if what I have is the same or if I'm mistaken? Thanks!

Im just guessing that your $domain probably contains .'s like mysite.com if that is the case you need to use preg_quote on the variable:
if (preg_match("/\.".preg_quote($domain, "/")."/", $referrer_url)) { }

If $domain is a regular string you may prefer to use strpos to Find the position of the first occurrence of a substring in a string. This would achieve the same result as using preg_quote with the benefit of being easier to read.
if (strpos($referrer_url, ".$domain") !== false) {
}

Related

How to parse a string of function parameters with REGEX in PHP

I am trying to handle parameters like Java or PHP natively handle them, using Regex to parse variable numbers (and types) of arguments. For example, a function might be:
util.echo(5, "Hello, world!");
In this instance, I would want to separate 5 as the first argument and "Hello, world!" as the second (without quotes). What I currently do is explode by commas, but that runs into issues if the string parameters include a comma. I don't have much experience with Regex, but I think it has some way of ignoring commas that are within quotes.
The Regex from this question (",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)") seems like it could work, but I'm confused on how to implement it with PHP.
To test a regular expression onto a string, you can use the preg_match() function in PHP.
See the manual
// $matches is going to output the matches
preg_match("/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/", $to_be_checked, $matches);
if($matches){
var_dump($matches);
// there was a match!
} else {
// the regular expression did not find any pattern matches
}
if you don't need to access the exact matches, just if there was at least one pattern match, you can simply do this:
if(preg_match("/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/", $to_be_checked)){
// there was a match!
} else {
// the regular expression did not find any pattern matches
}
Thank you to Zac and The fourth bird! Example solution that works, for future reference:
$parameters = 'util.echo(5, "Hello, world!");';
preg_match_all('/(?:[^\s(]+\(|\G(?!^),\s*)\K(?:"[^"]*"|[^,()]+)(?=[^()]*\);)/', $parameters, $matches);
if($matches){
var_dump($matches[0]);
} else {
echo('No matches.');
}

Selecting certain links with a REGEX

I'm working to do a "Wiki Game" with PHP, and i'd like to match all the links in a string starting by /wiki/something, for example /wiki/Chiffrement_RSA or /wiki/OSS_117_:_Le_Caire,_nid_d%27espions. I know just a few thigs about REGEX, so I'm struct. If someone could help me, it would be nice.
For the time, I just have \/wiki\/*...
Thanks for your help !
You can do by regex or strpos:
<?php
$mystring = 'abc';
$find = '/wiki/';
$statusLink = strpos($mystring, $find);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($statusLink === false) {
echo "Not the link that you want";
} else {
echo "You found the link";
}
//or by explode
$link = explode('/', $originalLink);
if ($link[1] == 'wiki' && isset($link[2])){
//is your link
}
?>
I don't use pure regex so much unless it's very necessary.
You can reduce your output array size by by 50% using \K in your pattern. It eliminates the need for a capture group and puts your desired substrings in the "fullstrings" array.
Pattern:
\/wiki\/\K[^ ]+
\K says "start the fullstring match from here". This means no memory waste. It may be a microimprovement, but I believe it to be best practice and I think more people should use it.
I finally chose Cody.code's answer with this regex : \/wiki\/([^ ]+).
I will use this code to check if i keep a link in an array or not (I will parse my html with DOMDocument an get all the <a>, it's faster) , so the preg_match() solution is the best for me, instead of strpos.
Thanks for your help !

Regex fomatting and design for a query

I'm having a some trouble formatting my regular expression for my PHP code using preg_match().
I have a simple string usually looking like this:
"q?=%23asdf".
I want my regular expression to only pass true if the string begins with "q?=%23" and there is a character at the end of the 3. So far one of the problems I have had is that the ? is being pulled up by the regex so doing something like
^q?=23 doesn't work. I am also having problems with contiguous searching in Regex expressions (because I can't figure out how to search after the 3).
So for clarification: "q?=%23asd" should PASS and "q?=%23" should FAIL
I'm no good with Regex so sorry if this seems like a beginner question and thanks in advance.
Just use a lookahead to check whether the character following 3 is an alphabet or not,
^q\?=%23(?=[a-zA-Z])
Add . instead of [A-Za-z] only if you want to check for any character following 3,
^q\?=%23(?=.)
Code would be,
$theregex = '~^q\?=%23(?=[a-z])~i';
if (preg_match($theregex, $yourstring)) {
// Yes! It matches!
}
else { // nah, no luck...
}
So the requirement is: Start with q?=%23, followed by at least one [a-z], the pattern could look like:
$pattern = '/^q\?=%23[a-z]+/i';
Used i (PCRE_CASELESS) modifier. Also see example at regex101.
$string = "q?=%23asdf";
var_dump(figureOut($string));
function figureOut($string){
if(strpos($string, 'q?=%23') == 0){
if(strlen($string) > 6){
return true;
}else{ return false;}
}
}

python regex to find php functions in files

I’m looking for a regular expression to match all functions blocks (from start to end) in php files. For example:
function test_function($var) {
if ($var == 'somethin') {
print 'hi';
}
etc.
}
I need the start offset and end offset of the block. What regex can I use?
It is very very complicated and can't be done with one regular expression.
You may think that you can easily match `the beginning of a function like this:
\bfunction\b\s+\S+[^\(](\s+)?\(.*?\)\s+\{
But you can't because what is if there is this in a code?
$string = "function myfunction() {}";
So you should search on everything what isn't quoted. So for excluding quoted strings you can use this regular expression:
(?:(?=(?:(?:\\.|"(?:\\.|[^"\\])*"|[^\\'"])*'(?:\\.|"(?:\\.|[^"'\\])*"|[^\\'])*')*(?:\\.|"(?:\\.|[^"\\])*"|[^\\'])*$)(?=(?:(?:\\.|'(?:\\.|[^'\\])*'|[^\\'"])*"(?:\\.|'(?:\\.|[^'"\\])*'|[^\\"])*")*(?:\\.|'(?:\\.|[^'\\])*'|[^\\"])*$)(?:\\.|[^\\'"]))+
The next thing you should do is counting all { and } because you need to know when the function stops and I can't think about any regular expression which can do this. So you need to do this with looping through.
Look at this: https://github.com/ramen/phply

compare portion of the string using php

I want to check whether the search keyword 'cli' or 'ent' or 'cl' word exists in the string 'client' and case insensitive. I used the preg_match function with the pattern '\bclient\b'. but it is not showing the correct result. Match not found error getting.
Please anyone help
Thanks
I wouldn't use regular expressions for this, it's extra overhead and complexity where a regular string function would suffice. Why not go with stripos() instead?
$str = 'client';
$terms = array('cli','ent','cl');
foreach($terms as $t) {
if (stripos($str,$t) !== false) {
echo "$t exists in $str";
break;
}
}
Try the pattern /cli?|ent/
Explanation:
cli matches the first part. The i? makes the i optional in the search.
| means or, and that matches cli, or ent.
\b is word boundary, It would not match cli in client, you need to remove \b

Categories