and like us on FB

In a couple of scheduled articles we had a need to evaluate syntax highlighted PHP code to determine the source of identified functions (PHP, WordPress, or our own) so we could linkify them back to the relevant page in the WordPress or PHP manual. Comparing the function against a large array (to determine where the function came from) proved problematic because the array came in at over 11,000 lines (for a feature not everybody would want to use). The solution ended up an easy one that we’ll talk about another time.

This article will provide you with code that will scrape the PHP manual and WordPress Codex pages, and return the referenced functions in an array. In addition, and simply for the sake of it, we’ve got a couple of WordPress functions that’ll render the function list from both manuals into a WP post or page. You can see examples of how we display the WP functions here and the PHP functions here. You may need to give both pages a few seconds to load (particularly the PHP page that weighs in at nearly 10,000 lines long).

The PHP Functions

The first function will construct an array of over 9000 functions and methods from the PHP manual. An example of the returned array is as follows: [key] => Description - FormattedLink.

The PHP function is as follows:

The code to build an array of WordPress functions in the format key => Link is as follows:

The WordPress Shortcode Functions

The WordPress shortcode is used to populate a page with a list of functions with links back to the relevant manual.

WordPress Codex

The first shortcode will list all the WordPress functions listed on the WP Function Reference page .

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

Use with the shortcode of [wpcodexfunctions].

PHP Functions

The second function will retrieve all the PHP functions listed on their Function and Method listing page.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

Use with the shortcode of [phpfunctions].

Considerations

  • Regular expressions are not normally rarely ever the most efficient solution for parsing HTML pages. PHP’s DomDocument class is almost always a better option. In my case, the regex was just easy. Obviously, if the format for listing the functions on either page changes then the regex will have to be updated.

    In terms of using regular expressions to parse HTML, the following post from Stackoverflow comes to mind.

Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)

  • That said, parsing arbitrary HTML is a bad idea. The PHP and WordPress Codex page have maintained structure for a long time.
  • It’s best to retrieve the data and insert into a database. This makes retrieval faster, and makes it super-simple to paginate into smaller chunks.
  • A secondary reason we wrote this functions was for the purpose of permitting free-text searches of all functions. An example screenshot is pictured below.

Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)

Searching for str_* functions

  • If you use shortcode and retrieve the results, understand that it’s a bad idea. Storing the returned data from the supplied functions in a database – or caching with our library to mitigate the high memory usage – is a must.

Download

Title: Get a List of WordPress and PHP Functions in an Array
Description: Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode).
Download: Shortcode (V0.2) | WP Plugin (V0.1) | PHP Code (V0.2)

Shortt URL for this post: http://shor.tt/35Ox