Php 将搜索与正则表达式和四行上下文匹配
例如,我有这样的代码:Php 将搜索与正则表达式和四行上下文匹配,php,regex,wordpress,Php,Regex,Wordpress,例如,我有这样的代码: <?php /** * Order * * The WooCommerce order class handles order data. * * @class WC_Order * @version 1.6.4 * @package WooCommerce/Classes * @category Class * @author WooThemes */ class WC_Order { /*
<?php
/**
* Order
*
* The WooCommerce order class handles order data.
*
* @class WC_Order
* @version 1.6.4
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Order {
/** @public int Order (post) ID */
public $id;
/** @public string Order status. */
public $status;
/** @public string Order date (placed). */
public $order_date;
/** @public string Order date (paid). */
public $modified_date;
/** @public string Note added by the customer. */
public $customer_note;
/** @public array Order (post) meta/custom fields. */
public $order_custom_fields;
global $wpdb, $woocommerce;
if ( empty( $type ) )
$type = array( 'line_item' );
if ( ! is_array( $type ) )
$type = array( $type );
$items = $this->get_items( $type );
$count = 0;
foreach ( $items as $item ) {
if ( ! empty( $item['qty'] ) )
$count += $item['qty'];
else
$count ++;
}
return apply_filters( 'woocommerce_get_item_count', $count, $type, $this );
}
/**
* Return an array of fees within this order.
*
* @access public
* @return array
*/
public function get_fees() {
return $this->get_items( 'fee' );
}
/**
* Return an array of taxes within this order.
*
* @access public
* @return void
*/
public function get_taxes() {
return $this->get_items( 'tax' );
}
/**
* Get taxes, merged by code, formatted ready for output.
*
* @access public
* @return void
*/
public function get_tax_totals() {
$taxes = $this->get_items( 'tax' );
$tax_totals = array();
foreach ( $taxes as $key => $tax ) {
$code = $tax[ 'name' ];
if ( ! isset( $tax_totals[ $code ] ) ) {
$tax_totals[ $code ] = new stdClass();
$tax_totals[ $code ]->amount = 0;
}
$tax_totals[ $code ]->is_compound = $tax[ 'compound' ];
$tax_totals[ $code ]->label = isset( $tax[ 'label' ] ) ? $tax[ 'label' ] : $tax[ 'name' ];
$tax_totals[ $code ]->amount += $tax[ 'tax_amount' ] + $tax[ 'shipping_tax_amount' ];
$tax_totals[ $code ]->formatted_amount = woocommerce_price( $tax_totals[ $code ]->amount );
}
return apply_filters( 'woocommerce_order_tax_totals', $tax_totals, $this );
}
/**
* has_meta function for order items.
*
* @access public
* @return array of meta data
*/
public function has_meta( $order_item_id ) {
global $wpdb;
return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, order_item_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d
ORDER BY meta_key,meta_id", absint( $order_item_id ) ), ARRAY_A );
}
/**
* Get order item meta.
*
* @access public
* @param mixed $item_id
* @param string $key (default: '')
* @param bool $single (default: false)
* @return void
*/
public function get_item_meta( $order_item_id, $key = '', $single = false ) {
return get_metadata( 'order_item', $order_item_id, $key, $single );
}
您不需要逐行循环文件数据。只需读取变量中的数据并应用正则表达式:
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/iphorm-form-builder';
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename) {
if (substr($filename, -3) == 'php') {
$context = shell_exec('grep -H -n -C4 do_action ' . escapeshellarg($filename));
if (!empty($context)) {
echo "found in $filename:";
echo "<pre>";
echo htmlentities($context);
echo "</pre>";
} else {
echo "failed reading to array";
}
}
}
这可以通过利用内置的shell命令非常简单地完成
Grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.
-C NUM, --context=NUM
Print NUM lines of output context. Places a line containing --
between contiguous groups of matches.
-H, --with-filename
Print the filename for each match.
-n, --line-number
Prefix each line of output with the line number within its input
file.
编辑
根据项目中有多少目录和文件,它可能无法执行。您基本上是在一个新的shell中为每个文件创建一个新进程。那不太好。如果您希望获得大量数据并稍后解析,请执行以下操作:
鉴于PHP正则表达式引擎的局限性,单靠正则表达式无法做到这一点。具体来说,这让我感到惊讶,你不能有可变长度的后面看
需要“向后看”的原因是,如果在连续行上出现了do\u action或apply\u filters,则如果没有“向后看”标题,则第一个匹配将阻止第二个匹配,即使使用了“向后看”标题,如果没有后面的外观,就无法获得第二场比赛的前两行。更不用说,我甚至不知道是否可以在结果中包含look-around断言的内容
该解决方案创建一个正则表达式来查找函数所在的行,然后使用另外两个正则表达式来查找函数前后的行。在设计正则表达式时,我注意文件的开头和结尾
这仅显示前两行和后两行。如果你想要更多,你可以使用重复,但在我看来,从尝试的解决方案来看,这正是o.p.真正想要的。请查看此线程,以获得一些关于如何更多地关注旧问题的建议:你给出的示例代码的预期输出是什么?啊,我想我明白了。您需要的术语是“上下文”。您需要一行匹配的内容,包含4行上下文。WTF<代码>全球$wpdb,$woocommerce代码>在class@EliasVanOotegem欢迎来到wordpress。在门口检查你的理智。@JoeFrambach:Yes的确$arr[3]将在匹配后打印4行
并且你对文件的前4行是正确的,现在检查编辑。哎呀,这真是太棒了,我现在正在用它构建一个插件:)内容目录可以在任何地方,如果在WP上下文中使用,这是更加防弹的:$path=WP\u CONTENT\u DIR'/插件/插件名称'代码>嗨,乔,很抱歉耽搁了你很长时间没有靠近电脑:-)非常感谢!!!
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/iphorm-form-builder';
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename) {
if (substr($filename, -3) == 'php') {
$context = shell_exec('grep -H -n -C4 do_action ' . escapeshellarg($filename));
if (!empty($context)) {
echo "found in $filename:";
echo "<pre>";
echo htmlentities($context);
echo "</pre>";
} else {
echo "failed reading to array";
}
}
}
Grep searches the named input FILEs (or standard input if no files are
named, or the file name - is given) for lines containing a match to the
given PATTERN. By default, grep prints the matching lines.
-C NUM, --context=NUM
Print NUM lines of output context. Places a line containing --
between contiguous groups of matches.
-H, --with-filename
Print the filename for each match.
-n, --line-number
Prefix each line of output with the line number within its input
file.
<?php
$path = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/iphorm-form-builder';
$grep = shell_exec('grep --include=*.php -RHn -C4 do_action ' . escapeshellarg($path));
$matches = explode("\n--\n", $grep);
if (empty($matches)) {
echo "failed reading to array";
}
else {
foreach ($matches as $match) {
echo "<pre>";
echo htmlentities($match);
echo "</pre>";
}
}
$offset = 0;
while (preg_match('/^.*?(?:apply_filters|do_action)\s*\(.+?\)\s*;.*(?:\n\r|\n|\r|$)/m', $file, $match, PREG_OFFSET_CAPTURE, $offset))
{
$index = $match[0][1];
$offset = $index + strlen($match[0][0]);
$hasBefore = preg_match('/(?:.*(?:\n\r|\n|\r).*$)|[^\n\r].*$/',
substr($file, 0, $index), $beforeMatch);
$hasAfter = preg_match('/^(?:.*(?:\n\r|\n|\r|$)){0,2}/',
substr($file, $offset), $afterMatch);
if ($hasBefore) print_r($beforeMatch);
print_r($match[0][0]);
if ($hasAfter) print_r($afterMatch);
}