Extract code/function by reading php file [duplicate] - php

This question already has answers here:
Parse PHP code to extract function names?
(3 answers)
Closed 8 years ago.
i've few PHP files and i want to read but not execute these files to fetch function list from them, i can use include and then get_class_methods by calling a class but i don't want to do that, i simply just want to read php file and extract function list, i tried some RegEx patterns but it also matches in strings which i dont want.
I used following RegEx pattern but dont want to use it though it dont seems that the solution would be RegEx:
preg_match_all("%function ([^(\s]+)%", $source, $func);`

Two options:
get regex buddy and play around with your regex until it does what it needs to
implement the following algorithm:
search for the string "function"
get the characters to the right of function until you encounter " " (whitespace)
when you encounter the "{" after the function name and arguments, declare $c = 1
for each "{" you encounter, increment $c
for each "}" you encounter, decrement $c
when $c == 0, you have reached the end of the function
save the function in a variable
repeat

Related

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

Capture group + numeral for PHP [duplicate]

This question already has answers here:
preg_replace: add number after backreference
(2 answers)
Closed 6 years ago.
I referred this question - Capture group reference + numeral
I tried with both the options provided there. Here is example code using ECMAScript expression (I guess I need to use this, correct me if I am wrong) -
$string = "box.last_rollout_revision = dummy";
$regex = "/(box\.last_rollout_revision[\s]*=[\s]*)[a-z0-9-_]*([\n]*)/";
$test = "234";
$replacementPattern = "$1".$test."$2";
echo preg_replace($regex,$replacementPattern,$string);
Expected output
box.last_rollout_revision = 234
Actual getting
34
P.S. I am trying to replace value against a key in a config file which has several other key-value pairs. I think regex will make it easier in this case. Correct me if I am wrong.
Change the replacement pattern to
$replacementPattern = '${1}'.$test.'$2';
See the IDEONE demo

Find & Replace MySQL [duplicate]

This question already has answers here:
MySQL string replace
(6 answers)
Closed 8 years ago.
I'd like to perform a find & replace in MySQL in GoDaddy's phpMyAdmin tool.
My table is called "wcswcd_posts", and I would like to find a URL, like:
http://wordpress.woodswcd.com/wordpress/wp-content/uploads/2013/11/drill.jpg
And replace it with:
/wordpress2/wp-content/uploads/2013/11/drill.jpg
Just not sure of what the syntax would be.
Thank you.
You are going to need to use the UPDATE statement & the REPLACE function
to change it try:
UPDATE wcswcd_posts SET url = REPLACE(url,'http://wordpress.woodswcd.com','')
REPLACE is a string function that replaces every occurrence of the 2nd string in the 1st string with the 3rd one.
By setting the 3rd argument in the REPLACE function to nothing, you will get rid of any occurence of http://wordpress.wordswcd.com leaving /wordpress/wp-content/uploads/2013/11/drill.jpg
This link should get you started.
Just replace http://wordpress.woodswcd.com/wordpress/ with http://wordpress.woodswcd.com/wordpress2/

PHP - How can I determine the numeric sequential value of a character in the english alphabet? [duplicate]

This question already has answers here:
How to convert some character into numeric in php?
(6 answers)
Closed 9 years ago.
Without using a loop, is there a way (function) to convert, for example, the letter "c" to its numeric sequence in the alphabet (3)?
I'm trying to take a literal string - $var = "c" and apply a function to it that returns 3.
Are there any built-in PHP functions that do this? I can't find any online and would rather avoid writing the function if necessary.
Anyone know of such a conversion function?
Yup.
$alphabet_position = ord(strtoupper($character)) - ord('A');

How to use strings with variables in codeigniter's language files? [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
for a localized website I want to create different language files.
But my main problem before starting the localiuation is, that i probably have strings with variables.
My theory is that i can use placeholders within my language files like:
$lang['somekey'] = "Hello Mr. %s, how are you?";
Is there a clean and nice way to parse those variables or do i have to develop a function for that?
Thanks.
I have the same problem and do it simply by using,
echo sprintf($this->lang->line('somekey'),'XYZ');//load language library before use
Read sprintf()
you can use codeigniter i18n with PHP .sprintf() to achieve what you want. load up the codeigniter non-variable strings (with those format stuff), then pass it on to .sprintf() for formatting and assignment of values. it should replace the %s part.
it's similar to this question. .sprintf() works like .printf(), only that it returns the string rather than printing it.

Categories