I am trying to profile a codeigniter application with xhprof. I am getting the report like following...
Now I am trying to ignore some function during xhprof report generation. For that what I did is like following....
$ignore = array(
'???_op',
'???_op#1',
'???_op#2',
'???_op#3',
'???_op#4',
'???_op#5'
);
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY, array('ignored_functions' => $ignore));
Now if I want to ignore all the CI related functions (i.e the functions starting like CI_*) seems like I have to insert them one by one in the array.
Is there any way where I can pattern match with regex and ignore functions according to my requirement?
Unfortunately, PHP's xhprof_enable() does not support regex patterns in the ignored_functions element of the options parameter.
I reckon the simplest way to manually generate the blacklist would be to copy-paste the rendered output from the function into your favorite IDE.
Once the text is in your IDE use the regex find/replace functionality to isolate your desired function names such as:
^(?:\?{3}_op|CI_)\S*
Then just copy the matches into your blacklist array.
Related
Yes, I know that people don't like parsing PHP, use a tokenizer they said, it will be great they said... I want you to know it isn't great, it isn't even fine.
I am working in .NET and using PCRE-NET and want to parse some PHP Functions to see if I can do some PHP tree shaking.
I tried using CodeParser which uses Antlr4 to tokenize, the results I got back were horrible to navigate. Yes it is all there technically, but it is so convoluted that really, Regex is better for what I am looking for.
I have the following regex working:
(?<functionScope>\w+)\s*function\s+(?<functionName>\w+)\s*\((?<functionArguments>(?:[^()]+)*)?\s*\)[\s:]*.*(?<functionBody>{(?:[^{}]+|(?-1))*+})
Try it out: https://regex101.com/r/yU6K45/1
This will break up a PHP File into the individual scopes, functions, arguments and function body. I am now looking at the functionBody and wanting to find all functions used inside that function, which I have here:
(?=[^\=\s])((?<functionClass>[$?\w[\w\d]*)?(?<ClassOperator>::|->|\\)?){0,3}?(?<functionName>\w[\w\d]*)\((?<Arguments>.*)?\)
See it at: https://regex101.com/r/3JzPR5/1
An issue I am having is with named groups. When there is a lot of namespacing, the named groups don't work out well. I am wondering if you have any ideas how to split up the line:
$uri = ExtraLevel\Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri);
To where I would have something like:
Full match ExtraLevel\Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri)
Group `functionClass` ExtraLevel\
Group `functionClass2` Psr7\
Group `functionClass3` UriResolver::
Group `functionName` resolve
Group `Arguments` Psr7\Utils::uriFor($config['base_uri']), $uri
Would love to match in a way that won't break when there aren't 3-4 levels.
This question might have an answer somewhere out there in the internet but I can't seem to find it. Of course if you have a link to give me I will gladly accept it as an answer if it is what I am looking for.
Here goes:
How can I exclude some strings from my global search ?
Basically I use the Ctrl + Shift + F to find all occurrences of a nameOfTheFile.php string to find all the patterns that call this file. A not so very smart developer created multiple nameOfTheFile.php everywhere and so the path to include them always changes, I need something fix so I need to change every single call. There is a lot of calls => 1533 occurrences according to PhpStorm so doing them one by one is NOT an option.
So my plan is to write all the patterns down (there shouldn't be more than 50 so it is doable) and replace all of them later. To do that I could use a filter to exclude the patterns that I have already found.
At the moment the pattern list would be something like:
include_once("folderY/nameOfTheFile.php");
include_once(PATH . "folderY/nameOfTheFile.php");
include_once (PATH . "folderY/nameOfTheFile.php");
include_once ("../../../folderX/nameOfTheFile.php");
include_once ("../../folderX/nameOfTheFile.php");
include_once ("../folderX/nameOfTheFile.php");
require_once($settings['siteFilepath'] . "folderY/nameOfTheFile.php");
How can I exclude those strings from the search? I thought of using a Regex but as I am not an expert (Junior Dev here :/) I can't really come up with it. Also I would think that maybe there is something built in PhpStorm that could work better.
Have I missed something ? Is there a Regex to help me ? Bonus point: if there is a Regex please explain how it works (remember I am far from being an expert).
in PhpStorm go to replace all cntr+shift+R
select 'Regex' option and enter
include_once.*nameOfTheFile\.php"\);
this will select offending entries for replacement
I want to get the class name or the ID from a text using regular expression with PHP.
For Instance:
.vc_custom_1547091583528{
margin-bottom:40px!important;
padding-top:7rem!important;
padding-bottom:1rem!important;
background:#092746 url(http://icoachu.us/wp-
content/uploads/2016/12/princeton.jpg?id=957)!important;
background-position:center!important;
background-repeat:no-repeat!important;
background-size:cover!important
}
This is css and the output should be:
array(0=>".vc_custom_1547091583528");
and also
array(0 => "http://icoachu.us/wp-content/uploads/2016/12/princeton.jpg?id=957")
So they will be two different functions.
Finally I have found the right answer. The regular expression can be made more smarter and short but right now this one works just fine.
([.#][a-zA-Z0-9_-]*?){[a-zA-Z0-9~;:#! _-]*\(['"]?(.*?)['"]?\)[a-zA-Z0-9~;:#! _-]*}
I'm using ctags on linux to create tags for source code using vim and the Tlist plug-in. The current ctags function parsing for PHP is woeful so i've downloaded the source for ctags and im going to change the regex that parses the functions.
Because i'm dealing with lots of code that has functions declared in many different ways i need a regex to reliably parse the function names properly.
Do you have one you could share that parses a php function name from a line of source code?
This is the current patched and 'improved' one from the ctags source which misses many functions, especially those marked as final or static first.
(^[ \t]*)(public[ \t]+|protected[ \t]+|private[ \t]+)?(static[ \t]+)?function[ \t]+&?[ \t]*([" ALPHA "_][" ALNUM "_]*)
Would just adding static and final to the possible list of words to ignore, and making it match more then one of the keywords be close enough?
(^[ \t]*)((public|protected|private|static|final)[ \t]*)*function[ \t]+&?[ \t]*([" ALPHA "_][" ALNUM "_]*)
Would mean it would accept junk like 'public public static final function bogus()', but php's syntax checking will reject it, and therefore shouldn't be a significant issue.
s/^.*\sfunction\s([^\(]*)\(.*$/\1/i
i tested it with some sed
grep -Ri function *| head -10 | sed 's/^.*\sfunction\s\([^\(]*\)(.*$/\1/i'
are there build in functions in latest versions of php specially designed to aid in this task ?
Use a DOM parser like SimpleXML to split the HTML code into nodes, and walk through the nodes to build the array.
For broken/invalid HTML, SimpleHTMLDOM is more lenient (but it's not built in).
String replace and explode would work if the HTML code is clean and always the same, as soon as you have new attributes it will brake.
So only dependable solution would be using regular expressions or XML/HTML parser.
Check http://php.net/manual/en/book.dom.php
An alternative to using a native DOM parser could be using YQL. This way you dont have to do the actual parsing yourself. The YQL Web Service enables applications to query, filter, and combine data from different sources across the Internet.
For instance, to grab the HTML table with the class example given at
http://www.w3schools.com/html/html_tables.asp
you can do
$yql = 'http://tinyurl.com/yql-table-grab';
$yql = json_decode(file_get_contents($yql));
print_r( $yql->query->results );
I've deliberated shortened the URL so it does not mess up the answer. $yql actually links to the YQL API, adds some options and contains the query:
select * from html
where xpath="//table[#class='example']"
and url="http://www.w3schools.com/html/html_tables.asp"
YQL can return JSON and XML. I've made it return JSON and decoded this then, which then results in a nested structure of stdClass objects and Arrays (so it's not all arrays). You have to see if that fits your needs.
You try out the interactive YQL console to see how it works.
i dont know if this is the faster , but you can check this class (using preg_replace)
http://wonshik.com/snippet/Convert-HTML-Table-into-a-PHP-Array
If you want to convert the html-description of a table, here's how I would do it:
remove all closing tags (</...>) ( http://php.net/manual/de/function.str-replace.php)
split string at opening tags (<...>) using a regular expression ( http://php.net/manual/en/function.split.php)
You have to work out the details on your own, since I do not know if you want to handle different lines as subarrays or you want to merge all lines into one big array or something else.
you could use the explode-function to turn the table cols and rows into arrays.
see: php explode