Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Regular expression for matching any of an array of tags? WordPress

  • SOLVED

Again, this is not so much a WordPress question as a PHP question, but my last question about regular expressions got a good answer here, so I'll ask again.

Assume I have an array with these values:

$arrayOfTemplateTags[] = 'TEMPLATE_TAG_1';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_2';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_3';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_4';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_5';


Assume I'm being given a block of text like this:

In June 2002 Mullenweg started using the b2/cafelog blogging software to complement the photos he was taking on a trip to Washington D.C. after participating in the National Fed Challenge competition. He contributed some minor code regarding typographic entities and cleaner permalinks. [[ TEMPLATE_TAG_2 ]] Several months after development of b2 had stopped, in January 2003, he announced on his blog his plan of forking the software to bring it up to date with web standards and his needs. [[ TEMPLATE_TAG_3 ]] He was quickly contacted by Mike Little and together they started WordPress from the b2 codebase. They were soon joined by original b2 developer Michel Valdrighi. Mullenweg was only nineteen years old, and a freshman at the University of Houston at the time. [[ echo $your_username . ' ' . $your_password ]]

Every time there is a [[ ]] block, I need to be sure that the only things inside the square brackets are some white space and one of the tags in the $arrayOfTemplateTags.

I just need a true/false answer. In other words, in the above block, the first 2 appearances of [[ ]] should answer 'true' but the 3rd appearance of [[ ]] should answer false.

What is the right regex for this?

Answers (2)

2010-11-27

Larrie Bernd Rocha answers:


preg_match_all ( '/\[\[.*?\]\]/', $string, $braces, PREG_SET_ORDER );
foreach ( $braces as $brace ){
$result = preg_match ( '/\[\[ (' . implode ( '|', $arrayOfTemplateTags) . ') \]\]/', $brace[0] );
echo (($result==1)?'true':'false');
}


Larrie Bernd Rocha comments:

I've tried and tested the answer above.
Simply provide the value for $string and $arrayOfTemplateTags array.
The output simply displays true or false consecutively. I'm sure you have your work around on this. =)


Lawrence Krubner comments:

I tried this following method, but I can not get $youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags to ever return false.


public function checkToSeeIfStringContainsPHPInsideSquareBrackets($string) {
$youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags = true;
// 2010-11-28 - this next block is meant to capture any attempt by hackers to insert arbitrary
// PHP code into the templates.
preg_match_all ( '/\[\[.*?\]\]/', $string, $braces, PREG_SET_ORDER );
foreach ( $braces as $brace ){
$result = preg_match ( '/\[\[ (' . implode ( '|', $this->arrayOfTemplateTags) . ') \]\]/', $brace[0] );
if ($result==1) $youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags = false;
}
return $youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags;
}






Lawrence Krubner comments:

I need it to react somehow to code like this:


<title>[[ TEMPLATE_TAG_6 ]] [[ TEMPLATE_TAG_7 echo \\\\\\"hate\\\\\\" ]] The is the dev site</title>


Lawrence Krubner comments:

When I add an "echo hi" I see that the function is correctly being called, but the preg_match_all seems to come back empty.

public function checkToSeeIfStringContainsPHPInsideSquareBrackets($string) {
$youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags = true;
// 2010-11-28 - this next block is meant to capture any attempt by hackers to insert arbitrary
// PHP code into the templates.
echo "hi";
preg_match_all ( '/\[\[.*?\]\]/', $string, $braces, PREG_SET_ORDER );
foreach ( $braces as $brace ){
$result = preg_match ( '/\[\[ (' . implode ( '|', $this->arrayOfTemplateTags) . ') \]\]/', $brace[0] );

echo $result . 'hi<br />';
if ($result==1) $youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags = false;
}
return $youCanTrustThisStringBecauseItOnlyHasAllowedTemplateTags;
}




Lawrence Krubner comments:

print_r($braces);

Simply returns an empty array


Lawrence Krubner comments:

Ah, my fault. I got it working.

2010-11-27

rilwis answers:

I'm not sure about data type you want to return. So I make a code that return an array, each element of this array will be true or false:


$arrayOfTemplateTags = array();
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_1';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_2';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_3';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_4';
$arrayOfTemplateTags[] = 'TEMPLATE_TAG_5';

$s = "In June 2002 Mullenweg started using the b2/cafelog blogging software to complement the photos he was taking on a trip to Washington D.C. after participating in the National Fed Challenge competition. He contributed some minor code regarding typographic entities and cleaner permalinks. [[ TEMPLATE_TAG_2 ]] Several months after development of b2 had stopped, in January 2003, he announced on his blog his plan of forking the software to bring it up to date with web standards and his needs. [[ TEMPLATE_TAG_3 ]] He was quickly contacted by Mike Little and together they started WordPress from the b2 codebase. They were soon joined by original b2 developer Michel Valdrighi. Mullenweg was only nineteen years old, and a freshman at the University of Houston at the time. [[ echo $your_username . ' ' . $your_password ]]";

// begin check
preg_match_all('#\[\[.*?\]\]#', $s, $m);
$m = $m[0];

$pat = '#\s*(' . implode('|', $arrayOfTemplateTags) . ')\s*#';

for ($i = 0, $len = count($m); $i < $len; $i++) {
$m[$i] = preg_match($pat, $m[$i]) ? true : false;
}
// end check

// test
print_r($m);
// output Array ( [0] => true [1] => true [2] => false)