Exclude strings from search PhpStorm - php

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

Related

Regex PHP Function

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.

PHP: Use a $_GET-Param with multiple other Params within a $_GET-Param

yeah, I know, the title is kind of confusing, but no better title came to my mind.
Here is my problem:
I want to use a link in my application, which would look like this:
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
The problem is that &someparam2 is meant to hang on the second $_GET-Param.
It would be like this:
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
Instead, PHP interprets that &someparam2 hangs on the first $_GET-Param.
localhost/index?jumpto=some_folder/somescript.php?someparam1=1234&someparam2=4321
Does anyone know a solution for this?
I already tried
localhost/index?jumpto='some_folder/somescript.php?someparam1=1234&someparam2=4321'
but of course that didn't work.
I hope you can understand my problem.
Thank you for your time.
You will need to URL encode your string some_folder/somescript.php?someparam1=1234 so that php will not parse & in the query string as a param separator.
use urlencode("some_folder/somescript.php?someparam1=1234");

PHP preg_replace markdown issue - detecting duplicates

In a project I am building I would like to use markdown as follows
*text* = <em>text</em>
**text** = <strong>text</strong>
***text*** = <strong><em>text</em><strong>
As those are the only three markdown formats I require, I would like to remain lightweight and avoid importing the entire PHP markdown library as that would introduce features I do not require and create issues.
So I have been trying to build some simple regex replaces. Using preg_replace I run:
'/(\*\*\*)(.*?)\1/' to '<strong><em>\2</em></strong>'
'/(\*\*)(.*?)\1/' to '<strong>\2</strong>'
'/(\*)(.*?)\1/' to '<em>\2</em>',
And this works great! em, bold, and the combo all work fine...
But if the user makes a mistake or enters to many stars, everything breaks.
i.e.
****hello**** = <strong><em><em>hello</em></strong></em>
*****hello***** = <strong><em><strong>hello</em></strong></strong>
******hello****** = <strong><em></em></strong>hello<strong><em></em></strong>
etc
When ideally it would create
****hello**** = *<strong><em>hello</em></strong>*
*****hello***** = **<strong><em>hello</em></strong>**
******hello****** = ***<strong><em>hello</em></strong>***
etc
Ignoring the un-required stars (so it would become clear to the user they made a mistake, and more importantly, the rendered HTML remains valid).
I presume there must be some way to modify my regex to do this but I cannot for the life of my work it out, even after a whole day trying!
I would also be happy with the result of
******hello****** = <strong><em>hello</em></strong>
So please, can anybody help me?
Also please consider uneven stars. In this case the below scenario would be ideal.
***hello* = **<em>hello</em>
And the time when a star should be part of the body and not detected, such as if a user inputs:
'terms and conditions may apply*'
or
'I give the film 5* out of 10'
Many many thanks
Try different capturing pattern (match anything except * one or more times),
'/(\*\*\*)([^*]+)\1/'

Changing/deleting html from file_get_contents

I'm currently using this code:
$blog= file_get_contents("http://powback.tumblr.com/post/" . $post);
echo $blog;
And it works. But tumblr has added a script that activates each time you enter a password-field. So my question is:
Can i remove certain parts with file_get_contents? Or just remove everything above the <html> tag? could i possibly kill a whole div so it wont load at all? And if so; how?
edit:
I managed to do it the simple way. By skipping 766 characters. The script now work as intended!
$blog= file_get_contents("powback.tumblr.com/post/"; . $post, NULL, NULL, 766);
After file_get_contents returns, you have in your hands a string. You can do anything you want to it, including cutting out parts of it.
There are two ways to actually do the cutting:
Using string functions like str_replace, preg_replace and others; the exact recipe depends on what you need to do. This approach is kind of frowned upon because you are working at the wrong level of abstraction, but in some cases it has an unmatched performance to time spent ratio.
Parsing the HTML into a DOM tree, modifying it appropriately (this time working at the appropriate level of abstraction) and then turn it back into a string and echo it. This can be more convenient to work with if your requirements are not dead simple and is easier to maintain, but it typically requires more code to be written.
If you want to do something that's most naturally expressed in HTML document terms ("cutting out this <div>") then don't be tempted and go with the second approach.
At that point, $blog is just a string, so you can use normal PHP functions to alter it. Look into these 2:
http://php.net/manual/en/function.str-replace.php
http://us2.php.net/manual/en/function.preg-replace.php
You can parse your output using simple html dom parser and display olythe contents thatyou really want to display

VIM: Show PHP function / class in command line?

Is there any way to show the current PHP function or class name in the VIM command line? I found a plugin for showing C function names in the status line but it does not work for PHP and in any case I prefer the command line be used to save valuable vertical lines.
Thanks.
EDIT
While looking for something completely unrelated in TagList's help I've just found these two functions:
Tlist_Get_Tagname_By_Line()
Tlist_Get_Tag_Prototype_By_Line()
Adding this in my statusbar works beautifully:
%{Tlist_Get_Tagname_By_Line()}
Also, did you read the Vim Wiki? It has a bunch of tips trying to adress the same need. There is also this (untested) plugin.
ENDEDIT
If you are short on vertical space maybe you won't mind using a bit of horizontal space?
TagList and TagBar both show a vertical list of the tags used in the current buffer (and other opened documents in TagList's case) that you can use to navigate your code.
However, I'm not particularly a fan of having all sorts of informations (list of files, VCS status, list of tags, list of buffers/tabs…) displayed at all times: being able to read the name of the function you are in is only useful when you actually need to know it, otherwise it's clutter. Vim's own [{ followed by <C-o> are enough for me.
I don't know anything about PHP, and I'm not trying to step on anyone's toes, but having looked at some PHP code I came up with this function which I think takes a simpler approach than the plugins that have been mentioned.
My assumpmtion is that PHP functions are declared using the syntax function MyFunction(){} and classes declared using class MyClass{} (possibly preceded by public). The following function searches backwards from the cursor position to find the most recently declared class or function (and sets startline). Then we search forward for the first {, and find the matching }, setting endline. If the starting cursor line is inbetween startline and endline, we return the startline text. Otherwise we return an empty string.
function! PHP_Cursor_Position()
let pos = getpos(".")
let curline = pos[1]
let win = winsaveview()
let decl = ""
let startline = search('^\s*\(public\)\=\s*\(function\|class\)\s*\w\+','cbW')
call search('{','cW')
sil exe "normal %"
let endline = line(".")
if curline >= startline && curline <= endline
let decl = getline(startline)
endif
call cursor(pos)
call winrestview(win)
return decl
endfunction
set statusline=%{PHP_Cursor_Position()}
Because it returns nothing when it is outside a function/class, it does not display erroneous code on the statusline, as the suggested plugin does.
Of course, I may well be oversimplifying the problem, in which case ignore me, but this seems like a sensible approach.

Categories