...

View Full Version : Regex replacing custom tags..



iLochie
02-24-2010, 03:00 AM
Hey everyone. I need some help with a regex function in my script.

I'm trying to replace content like: something {tag}hello{/tag} something else

The idea is that there can be anything surrounding those tags, and anything inside.

Using preg_replace, I want to remove the tags and the contents of the tags, but keep the outside stuff, and replace what used to be in the tags position.

Any help greatly appreciated! :)

JAY6390
02-24-2010, 04:25 AM
function tag_replace($matches) {
echo '<pre>'.print_r($matches, true).'</pre>';
}

$regex = '%\{([^}]+)\}(.*?)\{/\1\}%';
$text = 'something {tag}hello{/tag} something else';
preg_replace_callback($regex, 'tag_replace', $text);
In your function called tag_replace, $matches[1] will contain the tag type (tag in this example) and $matches[2] will contain the content (in this case hello)

To alter the text, simply return the text you wish to replace the tags

Here's an example of how to make an "img" tag

function tag_replace($matches) {
switch($matches[1]) {
case 'img':
return '<img src="'.$matches[2].'" alt="Image" />';
break;
default:
return $matches[0];
break;
}
}

$regex = '%\{([^}]+)\}(.*?)\{/\1\}%';
$text = 'something {img}http://www.abc.com/img.jpg{/img} something else';
echo preg_replace_callback($regex, 'tag_replace', $text);

Will produce
something <img src="http://www.abc.com/img.jpg" alt="Image" /> something else

iLochie
02-24-2010, 06:09 AM
Thanks Jay, but the way I was using before worked after I changed a few things, here's my regex. It's nothing special but it will get the job done for what I need:



$page = preg_replace("/(.*?)".$block[0]."(.*?)".$block[1]."(.*?)/is","\\1".$block_new."\\3",$page);


Where $block[0] is {tag} and $block[1] is {/tag} and $block_new replaces the code between $block[0] and $block[1].

cyrus709
02-24-2010, 09:14 AM
Here is an example of some code that i wrote for adding BB codes to a profile.

Note that the profile text is submitted on another page and recieved on this page, then redirected to the profile.
edit_profile_page < php_code < profile_page


<?php
//session
session_start();
if($_SESSION['logged'] != 1){ header("location:index.html"); }
$convert= $_SESSION['username'];
//connects to mysql
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
die();
}
//selects DB
mysql_select_db("text", $con);
//Gets text
$text = $_POST['profiletxt'];
/*strips html tags */
$text = strip_tags($text);
//text can not be longer then 45 words
$text = wordwrap($text, 45, "<br>");


/*specifies and replaces BB codes with appropriate html
currently all bb codes are here, some need to be taken out */

$text = preg_replace ('/\+)"\]/' , '<font color="\\1">' , $text );
$text = preg_replace ('/\[font size="([0-9]+)"\]/' , '<font size="\\1">' , $text );
$replace = array("", "", "", "", "", "", "", "", "", "", "");
$BBC = array("<b>", "</b>", "</font>", "<img src=http://localhost/Global%20C/profilegraph.php?username=", ">", "<i>", "</i>", "<u>", "</u>", "<img src=\"", "\">");

$text = str_replace($replace, $BBC, $text);
print $text;

mysql_query("UPDATE users SET Profile = '$text' WHERE username = '$convert'");
header("location: profile.php?username=" . $convert);
?>


In summary this script strips all html tags from the text, searches and replaces bb codes with proper html tags. This code obviously can not be copied and pasted, but can definitely be edited or at the least an example of how i would have done it ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum