473,856 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A (hopefully) simple regular expression question...

For anyone who can't be bothered to read my code and examples, scroll
to the bottom, the question's there. Thanks.

I'm using php and regular expressions to convert bbcode style things to
html. My code to convert something like this:

[quote Bob]
Hello there
[/quote]

to something like this:

<fieldset>
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>
</fieldset>

goes something like this:

$ret = preg_replace("/\[quote (.+?)\](.+?)\[\/quote\]/i", "\n
<fieldset>\n <legend>&nbsp;\ \1&nbsp;</legend>\n <div
class=\"quote\" >\\2</div>\n </fieldset>\n ", $ret);
Now I have some code that cuts the text at a certain point (in order to
get a general preview of a message) using this function:

function cut_post($strin g, $length = DEFAULT_CUT_LEN GTH) {
if (strlen($string ) > $length) { // if the string IS too long...
$ret = substr($string, 0, $length); // cut it

for ($i = strlen($ret) - 1; $i >= 0; $i--) { // going from the
end to the beginning
if (substr($ret, $i, 1) == "<") { // if the current
character is an html open tag (<)
if (substr($ret, $i + 1, 1) != "/") { // if we found
anything except "</" (i.e. an actual start tag)
$ret = substr($ret, 0, $i); // return everything up
to that point
$i = -1; // and set our pointer to before the
string starts so we don't carry on looking
} else {
$i = -1;
}
}
}

if (substr($ret, strlen($ret) - 1, 1) == " ")
$ret = substr($ret, 0, strlen($ret) - 1);

$ret .= "...";
} else { // if it's not too long...
$ret = $string; // don't cut it. amazing!
}

return $ret;
}
I have a problem when it just happens to cut half way through a long
quote the post cutter does its job and cuts everything from the last
HTML tag it may have been half way through (the <div> inside the
<fieldset>) but unfortunately doesn't account for the open <fieldset>
and it never gets closed as the post was cut there, ending up with HTML
similar to:

<fieldset>
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">. ..

and then the page continues, messing up my nice formatting. So my
question is:

Is there a way to use a regular expression to search for an open
<fieldset> and close it?
I'm pretty sure there is, but I'm not so good at those negative
regexps, I get that you'd have to search the output HTML for <fieldset>
followed by any string of characters and NOT </fieldset> and then if
that matches, then add </fieldset> to the end. Although that also
doesn't close the </div> although I'm not sure how it manages not to
close that (well actually cut it out) itself.

Hmm maybe I don't want to just close stuff at all... it might just be
best to remove the quote altogether or something. Anyway, can anyone
help? It's not a major problem, but it ruins my layout, and I like my
layout.

Well thanks to anyone who can help, and sorry I've rambled on. Just
trying to give as much info as I can.

Jan 12 '06 #1
6 1507
al************@ gmail.com wrote:

[snip]
Is there a way to use a regular expression to search for an open
<fieldset> and close it?
I'm pretty sure there is, but I'm not so good at those negative
regexps, I get that you'd have to search the output HTML for <fieldset>
followed by any string of characters and NOT </fieldset> and then if
that matches, then add </fieldset> to the end. Although that also
doesn't close the </div> although I'm not sure how it manages not to
close that (well actually cut it out) itself.

Hmm maybe I don't want to just close stuff at all... it might just be
best to remove the quote altogether or something. Anyway, can anyone
help? It's not a major problem, but it ruins my layout, and I like my
layout.

Well thanks to anyone who can help, and sorry I've rambled on. Just
trying to give as much info as I can.


You can use all the negative look-aheads you want, but if you are just
using it with preg_match in an if statement, you may be able to get away
with something along these lines:

$pattern='`<fie ldset>.*</fieldset>`isU';
if(preg_match($ pattern,$str)){
// fieldset is closed
}else{
// fieldset set is *not* closed
}

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Jan 12 '06 #2
Al
I just tested this and i get fieldset not closed for all 3 cases
(fieldset works properly, no fieldset was used at all and fieldset not
closed)

I was mainly testing to see whether it caught no fieldset used at all
as fieldset not closed.

Jan 12 '06 #3
Al wrote:
I just tested this and i get fieldset not closed for all 3 cases
(fieldset works properly, no fieldset was used at all and fieldset not
closed)


Really?? Here's what I get:

<?php
$str=array();
$str[]=<<<EOS
<fieldset>
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>
</fieldset>

EOS;

$str[]=<<<EOS
<fieldset>
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>

EOS;

$str[]=<<<EOS
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>

EOS;

$pattern='`<fie ldset>.*</fieldset>`isU';

foreach($str as $html){
echo $html,"---\n";
if(preg_match($ pattern,$html)) {
// fieldset is closed
echo 'fieldset closed',"\n\n";
}else{
// fieldset set is *not* closed
echo 'fieldset not closed',"\n\n";
}
}
?>
Result:
<fieldset>
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>
</fieldset>
---
fieldset closed

<fieldset>
<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>
---
fieldset not closed

<legend>&nbsp;B ob&nbsp;</legend>
<div class="quote">H ello there</div>
---
fieldset not closed
--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Jan 12 '06 #4
Al
I do apologise. It seems to work well, and I realised that my mistake
was in copying your code directly. the string in my code is $ret and
yours is $str. So basically I'm an idiot :)

Nonetheless, thanks for the help. And sorry I've been so long getting
back on the matter, I've been away for the weekend.

I'll implement the code as soon as I can and work out how to go about
fixing my output (whether I should cut the fieldset off entirely or
leave it and just ad trailing '...'s etc.)

I've thought of a few problems I could encounter. If I just leave the
'...'s and close the fieldset, I run the risk of one particular quote
being just long enough to finish the internal <div> but not the
fieldset, e.g.:

<fieldset>
<legend>Bob</legend>
<div>Just long enough to get the end of this in, but not the rest</div>
....

That'll be alright, but the '...'s won't be enclosed on the div, not
that that's a *major* problem, just if I want to style the divs more
than now, it won't style the '...' (although the same styling applied
to fieldset, fieldset * {} will work.

Anyway thanks again for helping me out.

Jan 16 '06 #5
Al wrote:
I do apologise. It seems to work well, and I realised that my mistake
was in copying your code directly. the string in my code is $ret and
yours is $str. So basically I'm an idiot :)

Nonetheless, thanks for the help. And sorry I've been so long getting
back on the matter, I've been away for the weekend.

I'll implement the code as soon as I can and work out how to go about
fixing my output (whether I should cut the fieldset off entirely or
leave it and just ad trailing '...'s etc.)

I've thought of a few problems I could encounter. If I just leave the
'...'s and close the fieldset, I run the risk of one particular quote
being just long enough to finish the internal <div> but not the
fieldset, e.g.:

<fieldset>
<legend>Bob</legend>
<div>Just long enough to get the end of this in, but not the rest</div>
...

That'll be alright, but the '...'s won't be enclosed on the div, not
that that's a *major* problem, just if I want to style the divs more
than now, it won't style the '...' (although the same styling applied
to fieldset, fieldset * {} will work.

Anyway thanks again for helping me out.


Rather than Using a fieldset, why not just use blockquote? Those are
meant for quoting others anyway.

--
Justin Koivisto, ZCE - ju****@koivi.co m
http://koivi.com
Jan 16 '06 #6
Al
I know, but I wanted something that had a title. The quotes look a bit
like this:

____
--| Al |---------------------
| ¯¯¯¯ |
| Hello there |
|______________ _______________ |

Currently a fieldset works quite nicely for this task, although I may
eventually move on to a blockquote with a border and a relatively
positioned (top + some pixels) span before it in HTML.

But thanks for the info, and I know my method wasn't exaclty
stylistically sound.

Jan 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2256
by: Reckless | last post by:
I've got a file with this in it: The data I'd like extracted is within the quotes: Some string data I can read the file out and extract (using string positions) the data I'd like but it would neater if I use a regular expression. Only problem is I've never seen a working example of this type of extraction and am completely new to PHP.
4
1618
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own. The following code works for a single word: def createStandaloneWordRegex(word): """ return a regular expression that can find 'peter' only if it's written alone (next to space, start of string, end of string, comma, etc) but
10
3052
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In other words, as the regular expression (....)($) matches the "4567" in the string "1234567", how would I create a similar regular expression that only matches the "45" in the same string. The same regular expression would match "32" in the string...
18
3049
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How ??
20
2575
by: Larry Woods | last post by:
I'm drawing a blank... What is the regular expression for search a string for NO occurances of a substring? Example: I want to find all lines that do NOT have the substing "image" in them. TIA, Larry Woods
5
3115
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address and this method does not.... Does anyone have an explanation? Function IsValidEmail(ByVal strIn As String) As Boolean
7
3836
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
6
2298
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not work: it seems that < is not a valid character, so the beginning of the word starts at theletter 'p' instead of '<'. Because I'm not an expert in regular expressions, maybe someone of you guys can help me? I need the correct regex to find the...
25
5192
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
0
9767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10806
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9544
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5773
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4189
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3214
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.