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
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;
?>

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.