I'm trying to get a list of all occurrences of a file being included in a php script.
I'm reading in the entire file, which contains this:
<?php
echo 'Hello there';
include 'some_functions.php';
echo 'Trying to find some includes.';
include 'include_me.php';
echo 'Testtest.';
?>
Then, I run this code on that file:
if (preg_match_all ("/(include.*?;){1}/is", $this->file_contents, $matches))
{
print_r($matches);
}
When I run this match, I get the expected results... which are the two include sections, but I also get repeats of the exact same thing, or random chunks of the include statement. Here is an example of the output:
Array (
[0] => Array ( [0] => include 'some_functions.php'; [1] => include 'include_me.php'; )
[1] => Array ( [0] => include 'some_functions.php'; [1] => include 'include_me.php'; ) )
As you can see, it's nesting arrays with the same result multiple times. I need 1 item in the array for each include statement, no repeats, no nested arrays.
I'm having some trouble with these regular expressions, so some guidance would be nice. Thank you for your time.
what about this one
<?php
preg_match_all( "/include(_once)?\s*\(?\s*(\"|')(.*?)\.php(\"|')\s*\)?\s*;?/i", $this->file_contents, $matches );
// for file names
print_r( $matches[3] );
// for full lines
print_r( $matches[0] );
?>
if you want a better and clean way, then the only way is php's token_get_all
<?php
$tokens = token_get_all( $this->file_contents );
$files = array();
$index = 0;
$found = false;
foreach( $tokens as $token ) {
// in php 5.2+ Line numbers are returned in element 2
$token = ( is_string( $token ) ) ? array( -1, $token, 0 ) : $token;
switch( $token[0] ) {
case T_INCLUDE:
case T_INCLUDE_ONCE:
case T_REQUIRE:
case T_REQUIRE_ONCE:
$found = true;
if ( isset( $token[2] ) ) {
$index = $token[2];
}
$files[$index] = null;
break;
case T_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE:
break;
default:
if ( $found && $token[1] === ";" ) {
$found = false;
if ( !isset( $token[2] ) ) {
$index++;
}
}
if ( $found ) {
if ( in_array( $token[1], array( "(", ")" ) ) ) {
continue;
}
if ( $found ) {
$files[$index] .= $token[1];
}
}
break;
}
}
// if your php version is above 5.2
// $files index will be line numbers
print_r( $files );
?>
Use get_included_files(), or the built-in tokenizer if the script is not included
I'm searching through a string of another files contents and not the
current file
Then your best bet is the tokenizer. Try this:
$scriptPath = '/full/path/to/your/script.php';
$tokens = token_get_all(file_get_contents($scriptPath));
$matches = array();
$incMode = null;
foreach($tokens as $token){
// ";" should end include stm.
if($incMode && ($token === ';')){
$matches[] = $incMode;
$incMode = array();
}
// keep track of the code if inside include statement
if($incMode){
$incMode[1] .= is_array($token) ? $token[1] : $token;
continue;
}
if(!is_array($token))
continue;
// start of include stm.
if(in_array($token[0], array(T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE, T_REQUIRE_ONCE)))
$incMode = array(token_name($token[0]), '');
}
print_r($matches); // array(token name, code)
Please read, how works preg_match_all
First item in array - it return all text, which is in regular expression.
Next items in array - that's texts from regular expression (in parenthesises).
You should use $matches[1]
Related
I have an array of arrays as such below and I want to check if the [avs_id] contains a substring "a_b_c". How to do this in php?
Array
(
[id] => 10003
[avs_id] => a_b_c_3248
)
Array
(
[id] => 10003
[avs_id] => d_e_f_3248
)
You can use array_filter():
$src = 'a_b_c';
$result = array_filter
(
$array,
function( $row ) use( $src )
{
return (strpos( $row['avs_id'], $src ) !== False);
}
);
3v4l.org demo
The result maintain original keys, so you can directly retrieve item(s) matching substring.
If you want only check if substring exists, or the number of items having substring, use this:
$totalMatches = count( $result );
Loop through your array and test for the string in the specific element of your array with strpos as in the example code below.
foreach($yourMainArray as $arrayItem){
if (strpos($arrayItem['avs_id'], 'a_b_c') !== false) {
echo 'true';
}
}
A loop may be more ideal but if you know what array index the string is in that you are after:
$arr = array('id'=>'10003', 'avs_id'=>'a_b_c_3248');
if (strpos($arr['avs_id'], 'a_b_c') !== false) {
echo 'string is in avs_id';
}
You can use :
foreach($yourArray as $arrayItem){
if (strpos($arrayItem['avs_id'], 'a_b_c') !== false) {
//return true : code here
}
}
All the errors I'm interested in debugging in Codeigniter's log files are reporting that they come from /system/core/Loader.php when they don't. For example, here's a line:
ERROR | 2014-09-22 22:35:43 | "Severity: Notice --> Undefined variable: my_variable \/system\/core\/Loader.php(829) : eval()'d code 84"
I know which file this is coming from, and it's a view. I'm aware of debug_backtrace and I'm thinking about making a string of it and concatenating it onto the end of the $msg variable in an overridden Log.php, but I first wanted to check two things with all you friends:
Is debug_backtrace the best way to do this? It returns a huge amount of data.
Is anyone aware of someone that's already done this? Seems like an obvious need for anyone using Codeigniter (...still ;)
Here's some code you could adapt to your needs (I recommend creating a My_Log and not editing the system file just in case CI does ever get an update). The reason I have this is complicated and not useful as-is any longer, but it might give you a start:
protected function log_query ($query, $type)
{
$backtrace = debug_backtrace ();
$backtrace = array_slice ( $backtrace, 2, -2 ); // first and last two elements are things that never change
$traced = array ();
foreach ( $backtrace as $bt )
{
$func = isset ( $bt['function'] ) ? $bt['function'].'()' : '';
$line = isset ( $bt['line'] ) ? $bt['line'] : '';
$file = isset ( $bt['file'] ) ? str_replace ( APP, '', $bt['file'] ) : '';
$object = isset ( $bt['object'] ) ? get_class ( $bt['object'] ) : '';
$args = array (); build_args ( $bt['args'], $args );
$args = implode ( '; ', $args );
if ( $object )
{
$obj_func = $object . '->' . $func;
} else {
$obj_func = $func;
}
$traced[] = "$file $obj_func $line $args";
unset($args);
}
$traced = implode ( "\n\t", $traced );
$date = MYSQL_DATE_TIME;
$uri = isset ( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '' ;
$query= preg_replace ( "/[\n\t]/", ' ', $query );
log_message ('error', "{$query}\n\n\t{$date} ({$type})\n\t{$uri}\n\t{$traced}\n--------------------------\n");
}
I have the following if statement
if ( $missCh[0]['type']==4 OR $missCh[1]['type'] == 4 OR $missCh[2]['type'] == 4 ) {
echo 'go ahead';
}
I would like to find out if $missCh[0] or [1] or [2] is meeting the statement so that I can then ask if($missCh[X]['value]==3) but I don't know which part of the array holds true.
$types = array( $missCh[0]['type'], $missCh[1]['type'], $missCh[2]['type'] );
foreach ( $types as $key => $val ) {
if ( $val == 4 ) $fours[] = $key;
}
foreach ( $types as $key => $val ) {
if ( in_array( $key, $fours ) ) continue;
if ( $val == 3 ) $threes[] = $key;
}
print_r( $fours );
print_r( $threes );
I would like to find out if $missCh[0] or [1] or [2] is meeting the statement
AS you currently have it, you're not going to be able to manage this within the script as you currently allow the echo to be actioned if ANY of the criteria return TRUE.
If you want to action something based on what a particular array value is individually, you'll need to check each individually and action as appropriate:
if($missCh[0]['type']==4)
{
//Do something
}
elseif ($missCh[1]['type']==4)
{
//Do something
}
elseif($missCh[2]['type']==4)
{
//Do something
}
If appropriate, your final elseif could be just else, which would then be a catch all if none of the previous checks returned TRUE.
I've isolated the responsibility to check the if in a function. That function echoes "go ahead" and return the index $miss[$i]['type'] which is equals to 4.
<?php
$miss[0]['type'] = 2;
$miss[1]['type'] = 4;
$miss[2]['type'] = 5;
function goAhead($miss) {
for($i=0;$i<=count($miss);$i++) {
if($miss[$i]['type']==4) {
echo 'go ahead';
return $i;
}
}
}
$i = goAhead($miss);
echo $i;
This solution will work with one or infinite indexes or $miss array. This means that you will never need to refactor this code if $miss array will growth.
I am writing a code for Bayesian Filter. For a particular word, I want to check if the word is in the stop words list or not, I populate from stop word list from a file on my pc.
Because I have to do this for many words I don't want to read the StopWord file from my pc again and again.
I want to do something like this
function isStopWord( $word ){
if(!isset($stopWordDict))
{
$stopWords = array();
$handle = fopen("StopWords.txt", "r");
if( $handle )
{
while( ( $buffer = fgets( $handle ) ) != false )
{
$stopWords[] = trim( $buffer );
}
}
echo "StopWord opened";
static $stopWordDict = array();
foreach( $stopWords as $stopWord )
$stopWordDict[$stopWord] = 1;
}
if( array_key_exists( $word, $stopWordDict ) )
return true;
else
return false;
}
I thought by using a static variable it will solve the issue, but it doesn't. Kindly help.
Put the static declaration at the beginning of the function:
function isStopWord( $word ){
static $stopWordDict = array();
if(!$stopWordDict)
{
$stopWords = file("StopWords.txt");
echo "StopWord opened";
foreach( $stopWords as $stopWord ) {
$stopWordDict[trim($stopWord)] = 1;
}
}
if( array_key_exists( $word, $stopWordDict ) )
return true;
else
return false;
}
This will work since an empty array is considered falsy.
I've been trying to build this recursive function for the better part of a day now, but I just can't seem to get it to work the way I want.
First, I have a property which holds some data that the function have to access:
$this->data
And then I have this string which the intention is to turn into a relative path:
$path = 'path.to.%id%-%folder%.containing.%info%';
The part of the string that are like this: %value% will load some dynamic values found in the $this->data property (like so: $this->data['id']; or $this->data['folder'];
and to make things really interesting, the property can reference itself again like so: $this->data['folder'] = 'foldername.%subfolder%'; and also have two %values% separated by a - that would have to be left alone.
So to the problem, I've been trying to make a recursive function that will load the dynamic values from the data property, and then again if the new value contains another %value% and so on until no more %value%'s are loaded.
So far, this is what I've been able to come up with:
public function recursiveFolder( $folder, $pathArr = null )
{
$newPathArr = explode( '.', $folder );
if ( count ( $newPathArr ) !== 1 )
{
foreach( $newPathArr as $id => $folder )
{
$value = $this->recursiveFolder( $folder, $newPathArr );
$resultArr = explode( '.', $value );
if ( count ( $resultArr ) !== 1 )
{
foreach ( $resultArr as $nid => $result )
{
$nvalue = $this->recursiveFolder( $result, $newPathArr );
$resultArr[$nid] = $nvalue;
}
}
$resultArr = implode( '.',$resultArr );
$newPathArr[$id] = $resultArr;
}
}
else
{
$pattern = '/%(.*?)%/si';
preg_match_all( $pattern, $folder, $matches );
if ( empty( $matches[0] ) )
{
return $folder;
}
foreach ( $matches[1] as $mid => $match )
{
if ( isset( $this->data[$match] ) && $this->data[$match] != '' )
{
$folder = str_replace( $matches[0][$mid], $this->data[$match], $folder );
return $folder;
}
}
}
return $newPathArr;
}
Unfortunately it is not a recursive function at all as it grinds to a halt when it has multiple layers of %values%, but works with two layers -barely-. (I just coded it so that it would work at a bare minimalistic level this point).
Here's how it should work:
It should turn:
'files.%folder%.blog-%type%.and.%time%'
into:
'files.foldername.blog-post.and.2013.feb-12th.09'
based on this:
$data['folder'] = 'foldername';
$data['type'] = 'post';
$data['time'] = '%year%.%month%-%day%';
$data['year'] = 2013;
$data['month'] = 'feb';
$data['day'] = '12th.%hour%';
$data['hour'] = '09';
Hope you can help!
Jay
I don't see the need for this too be solved recursively:
<?php
function putData($str, $data)
{
// Repeat the replacing process until no more matches are found:
while (preg_match("/%(.*?)%/si", $str, $matches))
{
// Use $matches to make your replaces
}
return $str;
}
?>