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
https://forums.phpfreaks.com/topic/95586-regular-expressions/
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
https://forums.phpfreaks.com/topic/95586-regular-expressions/#findComment-489337
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
https://forums.phpfreaks.com/topic/95586-regular-expressions/#findComment-489360
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
https://forums.phpfreaks.com/topic/95586-regular-expressions/#findComment-489375
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
https://forums.phpfreaks.com/topic/95586-regular-expressions/#findComment-489381
Share on other sites

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.