Jump to content

Regular expressions


xyn

Recommended Posts

Hey,

Can someone please help me with an if statement.

Basically In regular expressions i understan ?() is if then

and ?()| is if then else.

 

However when i use it in my bb code I dont understand how they work

Just wonder if someone could shwo me cupple of examples?

 

my code:

"/\[(color|Color|COLOR)=?()[/b[\](.*)/"

In my example, the bold text is my "variable" which i have got working.

however my if statment needs to read as follows:

 

If(word is between = and ]) set as var (.*)

 

thanks.

Link to comment
Share on other sites

whats that surpose to do!!!

also use the parameter /i to ignore case instead of trying to cover case versions (on color)

 

heres a simple BB to html code snip

your need to remove the extra - as if i didn't add them it will look messedup on this forum

 


<?php

// A simple FAST parser to convert BBCode to HTML
// Trade-in more restrictive grammar for speed and simplicty
//
// Syntax Sample:
// --------------
// [-img]http://phpfreaks.com/images/deadrats.gif[-/img]
// [-url="http://phpfreaks.com"]phpfreaks[-/url]
// [-mail="webmasterphpfreaks.com"]Webmaster[-/mail]
// [-size="25"]HUGE[-/size]
// [-color="red"]RED[-/color]
// [-b]bold[-/b]
// [-i]italic[-/i]
// [-u]underline[-/u]
// [-list][-*]item[-*]item[-*]item[-/list]
// [-code]value="123";[-/code]
// [-quote]John said yadda yadda yadda[-/quote]
//
// Usage:
// ------
// <?php include 'bb2html.php'; ?>
// <?php $htmltext = bb2html($bbtext); ?>



function bb2html($text)
{
  $bbcode = array("-<", "->",
                "[-list]", "[-*]", "[-/list]", 
                "[-img]", "[-/img]", 
                "[-b]", "[-/b]", 
                "[-u]", "[-/u]", 
                "[-i]", "[-/i]",
                '[-color="', "[-/color]",
                "[-size=\"", "[-/size]",
                '[-url="', "[-/url]",
                "[-mail=\"", "[-/mail]",
                "[-code]", "[-/code]",
                "[-quote]", "[-/quote]",
                '"]');
  $htmlcode = array("<", ">",
                "<ul>", "<li>", "</ul>", 
                "<img src=\"", "\">", 
                "<b>", "</b>", 
                "<u>", "</u>", 
                "<i>", "</i>",
                "<span style=\"color:", "</span>",
                "<span style=\"font-size:", "</span>",
                '<a href="', "</a>",
                "<a href=\"mailto:", "</a>",
                "<code>", "</code>",
                "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
                '">');
  $newtext = str_replace($bbcode, $htmlcode, $text);
  $newtext = nl2br($newtext);//second pass
  return $newtext;
}
?>

Link to comment
Share on other sites

thanks,

also I only want those cases

doesnt get replaced. i onyl want either or

 

as well as using preg_replace on windows with php 4.3.

 

Still would like some pointers on using an if statement too.

I like learning new things

Link to comment
Share on other sites

the 50 needs to be static.

the thing is when i use these quotes get mixed up..

so i tried grouping them and nothing. I recon the statement will work.

 

And I didnt actually know there was a specific forum for regular expressions.

Link to comment
Share on other sites

All i want to do is this...

or

and with regex i want to get the content between ="(.*)" as a variable.

however if i put [-color=red][-size=20] (exclude -) i get this

<span style='color:red"][size=16;'>

so i need an if statement to STOP the variable before ] which will stop this mix up

Link to comment
Share on other sites

this will be easier... to show you what im trying to do

<?
require_once "class.bbcode.php";
$bb =new BBcode;
$bb->bbSettings(array("bbenabled" =>0,"allowflash" =>true));
$s ="[color=\"red\"][size=\"16\"]Hey[/size] [strike]um[/strike][/color] [b]Name[/b],\n\n";
$s .="<br>How are [u][b][i]you[/i][/b][/u]; \n\n";
print $bb->bbDisplay($s);
?>

 

this is the class i was producing:

<?php
class BBcode {
function bbSettings($array =false){
	$this->bbCodeEnabled =$array['bbenabled'];
	$this->bbFlashEnabled =$array['allowflash'];
	$this->bbHyperlinkEnabled =$array['allowlinks'];
	$this->bbImagesEnabled =$array['allowimages'];
}
function bbDisplay($str){
	$bulktext =explode(" ",$str);
	foreach($bulktext as $word) $phrase[] =preg_replace($this->BBcode,$this->BBstyle,$word);
	return implode(" ",$phrase);
}
var $BBcode =array(
	"/\[[bb]\](.*)/","/\[\/[bb]\]/",
	"/\[[uu]\](.*)/","/\[\/[uu]\]/",
	"/\[[ii]\](.*)/","/\[\/[ii]\]/",
	"/\[(strike|Strike|STRIKE)\](.*)/","/\[\/(strike|Strike|STRIKE)\]/",
	"/\[(color|Color|COLOR)=\"(.*)\"\](.*)/","/\[\/(color|Color|COLOR)\]/",
	"/\[(size|Size|SIZE)=\"(\d+)\"\](.*?)/","/\[\/(size|Size|SIZE)\]/",

);
var $BBstyle =array(
	"<span style='font-weight:bold;'>\\1","</span>",
	"<span style='text-decoration:underline;'>\\1","</span>",
	"<span style='font-style:italic;'>\\1","</span>",
	"<span style='text-decoration:line-through;'>\\2","</span>",
	"<span style='color:\\2;'>\\3","</span>",
	"<span style='font-size:\\2pt;'>\\3","</span>"

);
var $BBReference =array("b","u","i","strike","color","colour");
}
?>

Link to comment
Share on other sites

you need lazyness on the color and strike

 

"/\[(color|Color|COLOR)=\"(.*?)\"\](.*?)/","/\[\/(color|Color|COLOR)\]/",

 

and

"/\[(strike|Strike|STRIKE)\](.*?)/","/\[\/(strike|Strike|STRIKE)\]/",

 

final code

<?php
//require_once "class.bbcode.php";
$bb =new BBcode;
$bb->bbSettings(array("bbenabled" =>0,"allowflash" =>true));

$s ="[color=\"red\"][size=\"16\"]Hey[/size] [strike]um[/strike][/color] [b]Name[/b],\n\n";
$s .="<br>How are [u][b][i]you[/i][/b][/u]; \n\n";
print $bb->bbDisplay($s);

class BBcode {
function bbSettings($array =false){
	$this->bbCodeEnabled =$array['bbenabled'];
	$this->bbFlashEnabled =$array['allowflash'];
	$this->bbHyperlinkEnabled =$array['allowlinks'];
	$this->bbImagesEnabled =$array['allowimages'];
}
function bbDisplay($str){
	$bulktext =explode(" ",$str);
	foreach($bulktext as $word) $phrase[] =preg_replace($this->BBcode,$this->BBstyle,$word);
	return implode(" ",$phrase);
}
var $BBcode =array(
	"/\[[bb]\](.*)/","/\[\/[bb]\]/",
	"/\[[uu]\](.*)/","/\[\/[uu]\]/",
	"/\[[ii]\](.*)/","/\[\/[ii]\]/",
	"/\[(strike|Strike|STRIKE)\](.*?)/","/\[\/(strike|Strike|STRIKE)\]/",
	"/\[(color|Color|COLOR)=\"(.*?)\"\](.*?)/","/\[\/(color|Color|COLOR)\]/",
	"/\[(size|Size|SIZE)=\"(\d+)\"\](.*?)/","/\[\/(size|Size|SIZE)\]/",

);
var $BBstyle =array(
	"<span style='font-weight:bold;'>\\1","</span>",
	"<span style='text-decoration:underline;'>\\1","</span>",
	"<span style='font-style:italic;'>\\1","</span>",
	"<span style='text-decoration:line-through;'>\\2","</span>",
	"<span style='color:\\2;'>\\3","</span>",
	"<span style='font-size:\\2pt;'>\\3","</span>"

);
var $BBReference =array("b","u","i","strike","color","colour");
}
?>

 

infact you should use it on BUI as well

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.