Code: Select all
$str10 = "#this is me#";
echo preg_match("/\#[^\n]+\#/", $matches, $str10);Moderator: General Moderators
Code: Select all
$str10 = "#this is me#";
echo preg_match("/\#[^\n]+\#/", $matches, $str10);Code: Select all
preg_match_all('@#(.*?)#@m',$str10,$matches);
print_r($matches);the first one returned with quotes and the next one without the quotes. wots the meaning of that??Array ( [0] => Array ( [0] => #this is me# ) [1] => Array ( [0] => this is me ) )
$matches[0] is an array of full full pattern matches found by the regex engine.raghavan20 wrote:the first one returned with quotes and the next one without the quotes. wots the meaning of that??Array ( [0] => Array ( [0] => #this is me# ) [1] => Array ( [0] => this is me ) )
m is multi-line mode, yes.raghavan20 wrote:does this 'm' denote "PCRE_MULTILINE"?
.*? is called an ungreedy matching. It will find the shortest text that fulfills the entire pattern. To illustrate:raghavan20 wrote:If you dont mind, can you say wots this (.*?)
Code: Select all
$text = 'this #is a string# with #two sets of hash markers#
here on the #second line# we have another hash marker';
// without the ?
preg_match_all('@#(.*)#@m',$text,$matches);
print_r($matches);
// with the ?
preg_match_all('@#(.*?)#@m',$text,$matches);
print_r($matches);Code: Select all
Array
(
[0] => Array
(
[0] => #is a string# with #two sets of hash markers#
[1] => #second line#
)
[1] => Array
(
[0] => is a string# with #two sets of hash markers
[1] => second line
)
)
Array
(
[0] => Array
(
[0] => #is a string#
[1] => #two sets of hash markers#
[2] => #second line#
)
[1] => Array
(
[0] => is a string
[1] => two sets of hash markers
[2] => second line
)
)Code: Select all
preg_match_all('/^#[^#]+#$/',$str10,$matches);
print_r($matches);
but it returns
//Array ( [0] => Array ( [0] => #this is me# ) )it can be potentially faster, but it's likely more to make it easier for nonregex people to understandraghavan20 wrote:When I was reading regex tutorials, they said that it is preferrable to use [^#] instead of .*? I dont really understand why???
it's not empty. preg_match_all() returns two dimensional arrays. You're looking at the zeroth element's array of data.raghavan20 wrote:I dont understand a few things from the result
1. why is the first array element empty?
raghavan20 wrote:2. how do I get the matched string excluded braces as you did with your regex expression .*?
Code: Select all
preg_match_all('/^#([^#]+)#$/',$str10,$matches);
print_r($matches);Code: Select all
<?php
$text = 'this #is a string# with #two sets of hash markers#
here on the #second line# we have another hash marker';
// without the ?
preg_match_all('@#([^#]+)#@m',$text,$matches);
print_r($matches);
// with the ?
preg_match_all('@#([^#]+?)#@m',$text,$matches);
print_r($matches);
?>Code: Select all
Array
(
[0] => Array
(
[0] => #is a string#
[1] => #two sets of hash markers#
[2] => #second line#
)
[1] => Array
(
[0] => is a string
[1] => two sets of hash markers
[2] => second line
)
)
Array
(
[0] => Array
(
[0] => #is a string#
[1] => #two sets of hash markers#
[2] => #second line#
)
[1] => Array
(
[0] => is a string
[1] => two sets of hash markers
[2] => second line
)
)I was asking why the first element was empty??? while yours returned something.raghavan20 wrote:
I dont understand a few things from the result
1. why is the first array element empty?it's not empty. preg_match_all() returns two dimensional arrays. You're looking at the zeroth element's array of data.
Code: Select all
#
#http://www.yourresourcecenter.com Backup software
#DATE: 27-08-2005 12:43:42
#
#Database :raghavan_Cms
#
CREATE DATABASE `raghavan_Cms`;
USE `raghavan_Cms`;
#Blogs_tbl
CREATE TABLE `Blogs_tbl` (
`BlogId` int(5) unsigned NOT NULL auto_increment,
`MemberId` varchar(5) NOT NULL default '',
`Title` varchar(50) NOT NULL default '',
`Description` text,
`Date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`BlogId`)
) TYPE=MyISAM;
#
#raghavan_Cms.Blogs_tbl: Table data
#
INSERT INTO raghavan_Cms.Blogs_tbl VALUES(1,'M1003','Beautiful','blog on all the beautiful places across the world!!!','2005-05-25');
#ChatMessages_tbl
CREATE TABLE `ChatMessages_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(11) unsigned NOT NULL default '0',
`FromId` int(10) unsigned NOT NULL default '0',
`RecipientId` int(11) unsigned default '0',
`Time` timestamp(14) NOT NULL,
`Message` varchar(255) NOT NULL default '',
PRIMARY KEY (`Id`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM;
#
#raghavan_Cms.ChatMessages_tbl: Table data
#
#ChatRooms_tbl
CREATE TABLE `ChatRooms_tbl` (
`Id` int(11) NOT NULL auto_increment,
`Name` varchar(25) NOT NULL default '',
`Description` varchar(100) default NULL,
`MaxUsersAllowed` int(11) NOT NULL default '200',
KEY `Id` (`Id`,`Name`,`MaxUsersAllowed`)
) TYPE=MyISAM;
#
#raghavan_Cms.ChatRooms_tbl: Table data
#
INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(1,'General Discussion','can talk about anything here',100);
INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(2,'Technology - Hardware','Can talk about hardware and networking',100);
INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(3,'Techonology - Software','Talk about any language, software or anything relevant to it',5);
#ChatUsers_tbl
CREATE TABLE `ChatUsers_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(10) unsigned NOT NULL default '0',
`UserName` varchar(20) NOT NULL default '',
`LastUpdate` timestamp(14) NOT NULL,
`SessionId` varchar(32) default NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `UserName` (`UserName`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM;
#
#raghavan_Cms.ChatUsers_tbl: Table data
#
INSERT INTO raghavan_Cms.ChatUsers_tbl VALUES(3,1,'Mini','20050826073821','df0aec64d22aa8808a449aac87af8483');
#Images_tbl
CREATE TABLE `Images_tbl` (
`ImageId` varchar(5) NOT NULL default '',
`Location` text NOT NULL,
PRIMARY KEY (`ImageId`)
) TYPE=MyISAM;
#
#raghavan_Cms.Images_tbl: Table data
#
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1001','/images/avatars/000.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1002','/images/avatars/001.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1003','/images/avatars/002.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1004','/images/avatars/003.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1005','/images/avatars/004.gif');Code: Select all
preg_match_all("/[\n\r][^#](.+);\n/m", $this->commands, $matches);
print_r($matches);Code: Select all
Array ( [0] => Array ( [0] => CREATE DATABASE `raghavan_Cms`; [1] => ) TYPE=MyISAM; [2] => INSERT INTO raghavan_Cms.Blogs_tbl VALUES(1,'M1003','Beautiful','blog on all the beautiful places across the world!!!','2005-05-25'); [3] => ) TYPE=MyISAM; [4] => ) TYPE=MyISAM; [5] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(1,'General Discussion','can talk about anything here',100); [6] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(3,'Techonology - Software','Talk about any language, software or anything relevant to it',5); [7] => ) TYPE=MyISAM; [8] => INSERT INTO raghavan_Cms.ChatUsers_tbl VALUES(3,1,'Mini','20050826073821','df0aec64d22aa8808a449aac87af8483'); [9] => ) TYPE=MyISAM; [10] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1001','/images/avatars/000.gif'); [11] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1003','/images/avatars/002.gif'); [12] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1005','/images/avatars/004.gif'); [13] => ) TYPE=MyISAM; [14] => ) TYPE=MyISAM; [15] => ) TYPE=MyISAM; [16] => INSERT INTO raghavan_Cms.MemberBlogMap_tbl VALUES('1','M1003'); [17] => INSERT INTO raghavan_Cms.MemberBlogMap_tbl VALUES('3','M1003'); [18] => INSERT INTO raghavan_Cms.MemberBlogMap_tbl VALUES('5','M1003'); [19] => ) TYPE=MyISAM; [20] => INSERT INTO raghavan_Cms.Members_tbl VALUES('M1001','raghavan','chock','raghavan20@rediffmail.com','0790790709','14, boughey rd','stoke','staffordshire','united kingdom','st15 9rd','I am a brave man and also enterprising.','2005-06-10','I1004'); [21] => INSERT INTO raghavan_Cms.Members_tbl VALUES('M1009','logesh','chitibabu','vaazstorm@yahoo.co.in','7944067945','117, college road','stoke on trent','Staffs','UK','st42ee','','2005-07-01','I1001'); [22] => INSERT INTO raghavan_Cms.Members_tbl VALUES('M1006','srinivaasan','sekhar','vaazstorm@yahoo.co.in','0790790790','117, college road','stoke - on - trent','staffordshire','united kingdom','st15 9rd','','2005-06-26','I1003'); [23] => INSERT INTO raghavan_Cms.Members_tbl VALUES('M1004','Diviya','Ganapathy','diviya@hotmail.com','0783007450','117, college road','stoke','staffordshire','united kingdom','st42ee','','2005-06-20','I1001'); [24] => INSERT INTO raghavan_Cms.Members_tbl VALUES('M1005','Murali','venugopal','murali@yahoo.com','0790790790','117, college road','stafford','staffordshire','united kingdom','st98 9df','','2005-06-24','I1001'); [25] => ) TYPE=MyISAM; [26] => INSERT INTO raghavan_Cms.Messages_tbl VALUES(1,'raghavan20','kannan','trial','hi, how are u?','Yes','2005-06-15'); [27] => INSERT INTO raghavan_Cms.Messages_tbl VALUES(3,'raghavan20','srinivasan','hi how do u do?','mate, its been a long time i mailed called in to see wot r u doing','No','2005-06-16'); [28] => INSERT INTO raghavan_Cms.Messages_tbl VALUES(5,'raghavan20','kannan','when is ur next increment','when do u expect to get ur next appraisal?','Yes','2005-06-16'); [29] => INSERT INTO raghavan_Cms.Messages_tbl VALUES(7,'raghavan20','raghavan20','try again','pls discard the message as its sent for trial purpose','Yes','2005-06-19'); [30] => INSERT INTO raghavan_Cms.Messages_tbl VALUES(9,'raghavan20','diviya','trial1','hi da','No','2005-06-21'); [31] => INSERT INTO raghavan_Cms.Messages_tbl VALUES(11,'raghavan20','raghavan20','message 4 ','again for trial','No','2005-06-23'); [32] => ','Yes','2005-06-23');Code: Select all
<?php
ob_start();
?>
#
#http://www.yourresourcecenter.com Backup software
#DATE: 27-08-2005 12:43:42
#
#Database :raghavan_Cms
#
CREATE DATABASE `raghavan_Cms`;
USE `raghavan_Cms`;
#Blogs_tbl
CREATE TABLE `Blogs_tbl` (
`BlogId` int(5) unsigned NOT NULL auto_increment,
`MemberId` varchar(5) NOT NULL default '',
`Title` varchar(50) NOT NULL default '',
`Description` text,
`Date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`BlogId`)
) TYPE=MyISAM;
#
#raghavan_Cms.Blogs_tbl: Table data
#
INSERT INTO raghavan_Cms.Blogs_tbl VALUES(1,'M1003','Beautiful','blog on all the beautiful places across the world!!!','2005-05-25');
#ChatMessages_tbl
CREATE TABLE `ChatMessages_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(11) unsigned NOT NULL default '0',
`FromId` int(10) unsigned NOT NULL default '0',
`RecipientId` int(11) unsigned default '0',
`Time` timestamp(14) NOT NULL,
`Message` varchar(255) NOT NULL default '',
PRIMARY KEY (`Id`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM;
#
#raghavan_Cms.ChatMessages_tbl: Table data
#
#ChatRooms_tbl
CREATE TABLE `ChatRooms_tbl` (
`Id` int(11) NOT NULL auto_increment,
`Name` varchar(25) NOT NULL default '',
`Description` varchar(100) default NULL,
`MaxUsersAllowed` int(11) NOT NULL default '200',
KEY `Id` (`Id`,`Name`,`MaxUsersAllowed`)
) TYPE=MyISAM;
#
#raghavan_Cms.ChatRooms_tbl: Table data
#
INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(1,'General Discussion','can talk about anything here',100);
INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(2,'Technology - Hardware','Can talk about hardware and networking',100);
INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(3,'Techonology - Software','Talk about any language, software or anything relevant to it',5);
#ChatUsers_tbl
CREATE TABLE `ChatUsers_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(10) unsigned NOT NULL default '0',
`UserName` varchar(20) NOT NULL default '',
`LastUpdate` timestamp(14) NOT NULL,
`SessionId` varchar(32) default NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `UserName` (`UserName`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM;
#
#raghavan_Cms.ChatUsers_tbl: Table data
#
INSERT INTO raghavan_Cms.ChatUsers_tbl VALUES(3,1,'Mini','20050826073821','df0aec64d22aa8808a449aac87af8483');
#Images_tbl
CREATE TABLE `Images_tbl` (
`ImageId` varchar(5) NOT NULL default '',
`Location` text NOT NULL,
PRIMARY KEY (`ImageId`)
) TYPE=MyISAM;
#
#raghavan_Cms.Images_tbl: Table data
#
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1001','/images/avatars/000.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1002','/images/avatars/001.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1003','/images/avatars/002.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1004','/images/avatars/003.gif');
INSERT INTO raghavan_Cms.Images_tbl VALUES('I1005','/images/avatars/004.gif');
<?php
$text = ob_get_clean();
preg_match_all('@^(?![\s#])\s*((?s:.*?))\s*;\s*$@m',$text,$matches);
print_r($matches);
?>Code: Select all
Array
(
[0] => Array
(
[0] => CREATE DATABASE `raghavan_Cms`;
[1] => USE `raghavan_Cms`;
[2] => CREATE TABLE `Blogs_tbl` (
`BlogId` int(5) unsigned NOT NULL auto_increment,
`MemberId` varchar(5) NOT NULL default '',
`Title` varchar(50) NOT NULL default '',
`Description` text,
`Date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`BlogId`)
) TYPE=MyISAM;
[3] => INSERT INTO raghavan_Cms.Blogs_tbl VALUES(1,'M1003','Beautiful','blog on all the beautiful places across the world!!!','2005-05-25');
[4] => CREATE TABLE `ChatMessages_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(11) unsigned NOT NULL default '0',
`FromId` int(10) unsigned NOT NULL default '0',
`RecipientId` int(11) unsigned default '0',
`Time` timestamp(14) NOT NULL,
`Message` varchar(255) NOT NULL default '',
PRIMARY KEY (`Id`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM;
[5] => CREATE TABLE `ChatRooms_tbl` (
`Id` int(11) NOT NULL auto_increment,
`Name` varchar(25) NOT NULL default '',
`Description` varchar(100) default NULL,
`MaxUsersAllowed` int(11) NOT NULL default '200',
KEY `Id` (`Id`,`Name`,`MaxUsersAllowed`)
) TYPE=MyISAM;
[6] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(1,'General Discussion','can talk about anything here',100);
[7] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(2,'Technology - Hardware','Can talk about hardware and networking',100);
[8] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(3,'Techonology - Software','Talk about any language, software or anything relevant to it',5);
[9] => CREATE TABLE `ChatUsers_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(10) unsigned NOT NULL default '0',
`UserName` varchar(20) NOT NULL default '',
`LastUpdate` timestamp(14) NOT NULL,
`SessionId` varchar(32) default NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `UserName` (`UserName`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM;
[10] => INSERT INTO raghavan_Cms.ChatUsers_tbl VALUES(3,1,'Mini','20050826073821','df0aec64d22aa8808a449aac87af8483');
[11] => CREATE TABLE `Images_tbl` (
`ImageId` varchar(5) NOT NULL default '',
`Location` text NOT NULL,
PRIMARY KEY (`ImageId`)
) TYPE=MyISAM;
[12] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1001','/images/avatars/000.gif');
[13] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1002','/images/avatars/001.gif');
[14] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1003','/images/avatars/002.gif');
[15] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1004','/images/avatars/003.gif');
[16] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1005','/images/avatars/004.gif');
)
[1] => Array
(
[0] => CREATE DATABASE `raghavan_Cms`
[1] => USE `raghavan_Cms`
[2] => CREATE TABLE `Blogs_tbl` (
`BlogId` int(5) unsigned NOT NULL auto_increment,
`MemberId` varchar(5) NOT NULL default '',
`Title` varchar(50) NOT NULL default '',
`Description` text,
`Date` date NOT NULL default '0000-00-00',
PRIMARY KEY (`BlogId`)
) TYPE=MyISAM
[3] => INSERT INTO raghavan_Cms.Blogs_tbl VALUES(1,'M1003','Beautiful','blog on all the beautiful places across the world!!!','2005-05-25')
[4] => CREATE TABLE `ChatMessages_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(11) unsigned NOT NULL default '0',
`FromId` int(10) unsigned NOT NULL default '0',
`RecipientId` int(11) unsigned default '0',
`Time` timestamp(14) NOT NULL,
`Message` varchar(255) NOT NULL default '',
PRIMARY KEY (`Id`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM
[5] => CREATE TABLE `ChatRooms_tbl` (
`Id` int(11) NOT NULL auto_increment,
`Name` varchar(25) NOT NULL default '',
`Description` varchar(100) default NULL,
`MaxUsersAllowed` int(11) NOT NULL default '200',
KEY `Id` (`Id`,`Name`,`MaxUsersAllowed`)
) TYPE=MyISAM
[6] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(1,'General Discussion','can talk about anything here',100)
[7] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(2,'Technology - Hardware','Can talk about hardware and networking',100)
[8] => INSERT INTO raghavan_Cms.ChatRooms_tbl VALUES(3,'Techonology - Software','Talk about any language, software or anything relevant to it',5)
[9] => CREATE TABLE `ChatUsers_tbl` (
`Id` int(10) unsigned NOT NULL auto_increment,
`ChatRoomId` int(10) unsigned NOT NULL default '0',
`UserName` varchar(20) NOT NULL default '',
`LastUpdate` timestamp(14) NOT NULL,
`SessionId` varchar(32) default NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `UserName` (`UserName`),
KEY `ChatRoomId` (`ChatRoomId`)
) TYPE=MyISAM
[10] => INSERT INTO raghavan_Cms.ChatUsers_tbl VALUES(3,1,'Mini','20050826073821','df0aec64d22aa8808a449aac87af8483')
[11] => CREATE TABLE `Images_tbl` (
`ImageId` varchar(5) NOT NULL default '',
`Location` text NOT NULL,
PRIMARY KEY (`ImageId`)
) TYPE=MyISAM
[12] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1001','/images/avatars/000.gif')
[13] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1002','/images/avatars/001.gif')
[14] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1003','/images/avatars/002.gif')
[15] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1004','/images/avatars/003.gif')
[16] => INSERT INTO raghavan_Cms.Images_tbl VALUES('I1005','/images/avatars/004.gif')
)
)