I have to create translations for the project I work on. The simplest solution was to change all stings to a functioncall(string) so that I could get unique string hashes everytime.
My code has the following different t() function uses:
<label for="anon"><?php echo t('anonymously? (20% extra)')?></label>
exit(t("Success! You made {amount} oranges out of it!", array('amount' => $oranges)));
echo t('You failed.');
My current regexp is:
$transMatches = preg_match_all('/[^a-z]t\([^)(]+/m', $contents, $matches);
The problem is that it fails on #1 example, matchin "anonymously?".
What I really want to achieve is: "match t( then match either ' or " then match anything except what you matched for ' or " and )"
Idea: t\(['|"](.*?)[^'|"]\)?
I cannot make above regexp to work.
How could I do AND in regexp so that it matches "['|"] AND )" OR "['|"] AND, array"
Please help me on regexp and explain why it works.
Thank you!
Parsing function arguments may be quite complex, but you need to parse only the first argument which (for simplicity) can assume always to be string escaped either with ' or with ", thus those regexps may match"
"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"
\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'
Therefore you just need to match:
'~[^\w\d]t\(\s*("[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\')~i'
[^\w\d] assumes that no test1t will match, \s* makes you space tolerant...
With this regexp you'll get results like:
'anonymously? (20% extra)'
"Success! You made {amount} oranges out of it!"
And I can't imagine situation where you would need to parse out array too, can you describe it in comment/question?
Is this what you need?
Using Backreferences in The Regular Expression - http://www.regular-expressions.info/brackets.html
But it looks strange what are you doing, and why?
Are you replacing the function call with some result? why dont you just let it call the function and return translation from it?
Related
Using PHP, I am trying to test for the presence of various words and patterns in a string but am not able to figure out why I am seeing odd behaviour when attempting to match certain words.
Example 1:
Why does the following not return 1?
$test = 'clen=a.length;for(i=0;i<clen;i++)b+=String.fromCharCode(a.charCodeAt(i)^2)';
$result = preg_match('/(string)/i', $test, $matches);
$result is always zero for the above even though the word "String" is present in the subject string.
Example 2:
However, let's say I slightly change my regex to the following:
$test = 'clen=a.length;for(i=0;i<clen;i++)b+=String.fromCharCode(a.charCodeAt(i)^2)';
$result = preg_match('/st.+(ring)/i', $test, $matches);
The above returns the value of 1 for $result. Seems like when I split up the word "string" into separate parts, I can get a match.
Example 3:
Once again when I slightly modify the regex in this example, it also returns zero but I'm not sure why:
$test = 'clen=a.length;for(i=0;i<clen;i++)b+=String.fromCharCode(a.charCodeAt(i)^2)';
$result = preg_match('/(tring)/i', $test, $matches);
Trying to match on the sequence of characters such as "tring" returns 0 but when matching on "ring" it returns 1. But "tring" doesn't sound like any type of special or reserved word!
This behaviour is also the same for various other words such as "document" and "unescape" and I'm sure there are many others.
I am assuming that some words are probably being treated differently by the regex engine because they might be reserved or special in some way but I have not been able to find an official explanation for the above behaviour.
I apologise if I am missing something really obvious and would really appreciate it if someone can please explain this to me.
Many thanks.
i think your first regex is fine. Look here
https://regex101.com/r/tO9vN8/1
But there seems to be a Problem with the charset, i had to rewrite the expression - if i copy from this site, the regex did not match.
I hope this will be the right direction ...
i am trying to get this
string = 'Boss S02E06 more string'
i want S02E06 out of the complete string . and if possible split the string there.
Please note that the values after S and E in that keep changing so its not constant .
But im not expert when it comes to it.But i guess its time to start learning.
Thanks guys
try this:
$string = "Boss S02E06 more string";
preg_match(`.*(S\d+E\d+).*`,$string,$match);
echo $match[0];// it will echo S02E06
You would use a Regular Expression. You can learn them here.
PHP has preg_match(), which is what you would use.
The regex to match would be something like S\d+E\d+. If you want the values, use a capturing group.
It's up to you to put all these ideas together to arrive at an answer.
Basically I want to find a function from a php string source content. I'm trying to parse a php file and read its content into string. I want to find something like:
function_name(paras) or function_name() or function_name(params, params)
for example if source contains:
echo 'Greetings'.greet("I'm Johan");
$age = date_of_birth(date());
echo 'I am ' .$age . 'years old';
it would then find greet, date_of_birth, date because these are the functions used.
If you want to get the parameters including nested brackets, like your date_of_birth(date()), its maybe not impossible with regex but very difficult.
If you say its enough to find the name of the function then you can try this:
\w+(?=\()
See it here on Regexr
That will match at least one word character that is followed by an opening bracket.
\w contains letters, digits and the underscore
(?=\() is a positive look ahead that checks if a ( is following
you need a regular expression:
maybe something like this
preg_match("[a-zA-Z_][a-zA-Z_0-9]*(.*)",$stringToLookIn);
preg_match_all('/([a-zA-Z_]\w+)\s*\(/', $source, $match);
var_dump($match[1]);
I have a string like this: Hello #"user name". Where are you from, #"user name"?
I need to get the string between the " statements (user name), but I don't know how to do it.
I tried something like this /#("(.*)"|(.[^ ]*))\s*/ but it works wrong
First off, one possible regular expression that grabs the data you need is #"(.+?)", which matches any data within quotes preceded by #, and captures the data inside. Now that you've added the regex you've tried, I'm betting that the issue is that your expression is greedy: the regex engine tries to grab the longest match possible, so returns all of #"user name". Where are you from, #"user name". Adding the ? makes the expression lazy, so it will grab the shorter match.
Since you're interested in the content inside, I'm guessing that your final goal is to replace those strings with various types of user data dynamically, so one approach would be preg_replace_callback:
function user_data($matches) {
$key = $matches[1];
// return the user data for a $key like "user name"
}
$output = preg_replace_callback('/#"(.+?)"/', 'user_data', $input);
try looking at this: http://www.php.net/manual/en/function.strstr.php you might need to explode the white space after and get the first item from the array as well.
If there is only one #"..." per string, something like this should work
$matches = array();
preg_match("/#\"(.+?)\"/i", $inputstring, $matches);
echo($matches[1]);
Try this, if its not working, just escape " in pattern
/\#\"e\;([\w\s]{0,})\"e\;/
It's been several years since I have used regular expressions, and I was hoping I could get some help on something I'm working on. You know how google's search is quite powerful and will take stuff inside quotes as a literal phrase and things with a minus sign in front of them as not included.
Example: "this is literal" -donotfindme site:examplesite.com
This example would search for the phrase "this is literal" in sites that don't include the word donotfindme on the webiste examplesite.com.
Obviously I'm not looking for something as complex as Google I just wanted to reference where my project is heading.
Anyway, I first wanted to start with the basics which is the literal phrases inside quotes. With the help of another question on this site I was able to do the following:
(this is php)
$search = 'hello "this" is regular expressions';
$pattern = '/".*"/';
$regex = preg_match($pattern, $search, $matches);
print_r($matches);
But this outputs "this" instead of the desired this, and doesn't work at all for multiple phrases in quotes. Could someone lead me in the right direction?
I don't necessarily need code even a real nice place with tutorials would probably do the job.
Thanks!
Well, for this example at least, if you want to match only the text inside the quotes you'll need to use a capturing group. Write it like this:
$pattern = '/"(.*)"/';
and then $matches will be an array of length 2 that contains the text between the quotes in element 1. (It'll still contain the full text matched in element 0) In general, you can have more than one set of these parentheses; they're numbered from the left starting at 1, and there will be a corresponding element in $matches for the text that each group matched. Example:
$pattern = '/"([a-z]+) ([a-z]+) (.*)"/';
will select all quoted strings which have two lowercase words separated by a single space, followed by anything. Then $matches[1] will be the first word, $matches[2] the second word, and $matches[3] the "anything".
For finding multiple phrases, you'll need to pick out one at a time with preg_match(). There's an optional "offset" parameter you can pass, which indicates where in the string it should start searching, and to find multiple matches you should give the position right after the previous match as the offset. See the documentation for details.
You could also try searching Google for "regular expression tutorial" or something like that, there are plenty of good ones out there.
Sorry, but my php is a bit rusty, but this code will probably do what you request:
$search = 'hello "this" is regular expressions';
$pattern = '/"(.*)"/';
$regex = preg_match($pattern, $search, $matches);
print_r($matches[1]);
$matches1 will contain the 1st captured subexpression; $matches or $matches[0] contains the full matched patterns.
See preg_match in the PHP documentation for specifics about subexpressions.
I'm not quite sure what you mean by "multiple phrases in quotes", but if you're trying to match balanced quotes, it's a bit more involved and tricky to understand. I'd pick up a reference manual. I highly recommend Mastering Regular Expressions, by Jeffrey E. F. Friedl. It is, by far, the best aid to understanding and using regular expressions. It's also an excellent reference.
Here is the complete answer for all the sort of search terms (literal, minus, quotes,..) WITH replacements . (For google visitors at the least).
But maybe it should not be done with only regular expressions though.
Not only will it be hard for yourself or other developers to work and add functionality on what would be a huge and super complex regular expression otherwise
it might even be that it is faster with this approach.
It might still need a lot of improvement but at least here is a working complete solution in a class. There is a bit more in here than asked in the question, but it illustrates some reasons behind some choices.
class mySearchToSql extends mysqli {
protected function filter($what) {
if (isset(what) {
//echo '<pre>Search string: '.var_export($what,1).'</pre>';//debug
//Split into different desires
preg_match_all('/([^"\-\s]+)|(?:"([^"]+)")|-(\S+)/i',$what,$split);
//echo '<pre>'.var_export($split,1).'</pre>';//debug
//Surround with SQL
array_walk($split[1],'self::sur',array('`Field` LIKE "%','%"'));
array_walk($split[2],'self::sur',array('`Desc` REGEXP "[[:<:]]','[[:>:]]"'));
array_walk($split[3],'self::sur',array('`Desc` NOT LIKE "%','%"'));
//echo '<pre>'.var_export($split,1).'</pre>';//debug
//Add AND or OR
$this ->where($split[3])
->where(array_merge($split[1],$split[2]), true);
}
}
protected function sur(&$v,$k,$sur) {
if (!empty($v))
$v=$sur[0].$this->real_escape_string($v).$sur[1];
}
function where($s,$OR=false) {
if (empty($s)) return $this;
if (is_array($s)) {
$s=(array_filter($s));
if (empty($s)) return $this;
if($OR==true)
$this->W[]='('.implode(' OR ',$s).')';
else
$this->W[]='('.implode(' AND ',$s).')';
} else
$this->W[]=$s;
return $this;
}
function showSQL() {
echo $this->W? 'WHERE '. implode(L.' AND ',$this->W).L:'';
}
Thanks for all stackoverflow answers to get here!
You're in luck because I asked a similar question regarding string literals recently. You can find it here: Regex for managing escaped characters for items like string literals
I ended up using the following for searching for them and it worked perfectly:
(?<!\\)(?:\\\\)*(\"|')((?:\\.|(?!\1)[^\\])*)\1
This regex differs from the others as it properly handles escaped quotation marks inside the string.