Jump to content

Creating Short Tags / Code Snippets


NickV

Recommended Posts

Hi guys,

 

In a CMS I want the user to be able to enter a short tag / snippet such as:

 

[tag_name page=page_name' length='100]

 

Where tag_name is the name of the short tag and page and length are 2 parameters.

 

I need to check whether this tag has been added to the content and extract the values of the 2 parameters.

 

I'm not sure exactly how to do this. I thought preg_match() would be of some use but I'm useless at regular expressons  :-\

 

Thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/238770-creating-short-tags-code-snippets/
Share on other sites

I´m not sure do you mean this but:

 

<?php
$content = "Some Content Here";

$from = array("/\[tag_name page=\'(.*?)\' length=\'(.*?)\'\]/is");
$to = array("Tag name $1, length $2");

$content = preg_replace($from, $to, $content);

print $content;
?>

 

If you want to execute a function then it would go

 

<?php
$content = "Some Content Here";

function someFunction($var1, $var2){
// do something with the var1 & var2  
}

$from = array("/\[tag_name page=\'(.*?)\' length=\'(.*?)\'\]/e");
$to = array('someFunction("$1", "$2")');

$content = preg_replace($from, $to, $content);

print $content;
?>

That great, thanks  :D

 

I changed it a bit to:

 

<?php
$pattern = "/\[tag_name page=(.*?) length=(.*?)\]/is";					    
preg_match($pattern, $this->content, $matches);		
echo $matches[1]." ".$matches[2];
?>

 

to extract the parameters from the content. I also removed the apostrophes from the parameters so it will search for:

 [tag_name page=page_name length=100] 

 

Cheers

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.