Question

What I am trying to accomplish is a rather straightforward regex pattern that will eventually scrape the contents of a page/custom post type. For the time being I am simply checking against a single line string.

The following RegEx pattern is valid (copy and paste into RegExr - http://regexr.com).

$pattern = "/\[jwplayer(.+?)?\]?]/g"; 
$videoTest = "[jwplayer config=\"top_10_videos\" mediaid=\"107\"]";
preg_match($videoTest, $pattern, $matches);
print_r($matches);`

However the output is the following:

Array
(
    [0] => Array
        (
        )
)

I've tested other regex patterns (simple ones) and I've scoured the net (including stack overflow) for an answer to this specific problem but haven't been successful in solving the issue. The php code above has been placed inside of functions.php of WordPress v 3.5 if that information helps and is called using a 'wp_ajax' hook. The ajax hook is working as expected.

Any help anyone can provide would be great!

Thanks, Nick

No correct solution

OTHER TIPS

The g modifier is not used in PHP. Use preg_match_all() instead.

Further more, the arguments for preg_match are in the wrong order. The arguments need to be in this order:

preg_match($pattern, $videoTest, $matches);

Read the Regular Expressions documentation.

A more robust way of retrieving stuff from a string using regular expressions it to be as specific as possible. This prevents malformed stuff from getting through. For example:

function getJQPlayer($string) {
    $pattern = '/\[jwplayer(?:\s+[\w\d]+=(["\'])[\w\d]+\\1)*\]/';
    preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);
    foreach ($matches as & $match) {
        $match = array_shift($match);
    }
    return $matches ?: FALSE;
}
$videoTest  = "[jwplayer config=\"top_10_videos\" mediaid=\"107\"]";
$videoTest .= ",[jwplayer config=\"bottom_10_videos\" mediaid=\"108\"]";
echo '<pre>', print_r(getJQPlayer($videoTest), true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top