473,811 Members | 4,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression HELP!

jn
I'm stripping out the attributes in <TD> tags...but I want to strip out
everything BUT the COLSPAN attribute.

The following strips out all attributes. What do I do if I want to keep a
certain one?

eregi_replace(" <TD[^>]*>","<TD>", $string);

I suck at regular expressions. I need some help.

Thanks
Jul 17 '05 #1
9 3246
jn wrote:
I'm stripping out the attributes in <TD> tags...but I want to strip out
everything BUT the COLSPAN attribute.

The following strips out all attributes. What do I do if I want to keep a
certain one?

eregi_replace(" <TD[^>]*>","<TD>", $string);


This is what I had used one time long ago:

preg_match_all( '/<td(\s([^>]+)*)>/i',$subject,$at tributes);
preg_match_all( '/[a-z]+\s*=\s*(\'|")? ([^\'"]*)\\1/i',$attributes[2][0],$attributes);
$attributes=$at tributes[0];

I'm sure someone will have something better though...

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #2
jn wrote:
I'm stripping out the attributes in <TD> tags...but I want to strip out
everything BUT the COLSPAN attribute.

The following strips out all attributes. What do I do if I want to keep a
certain one?

eregi_replace(" <TD[^>]*>","<TD>", $string);


preg_replace is faster and more powerful

I tried this:
<?php

$data = '===<td a="b" colspan="3" x="y">===';

$regex = '<td([^>]*)( colspan=\S+)([^>]*)>';

$newdata = preg_replace("/$regex/i", '<td$2>', $data);

echo $newdata, "\n";

?>
The output was:
===<td colspan="3">===
HTH
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #3
jn
"Pedro" <he****@hotpop. com> wrote in message
news:bo******** *****@ID-203069.news.uni-berlin.de...
jn wrote:
I'm stripping out the attributes in <TD> tags...but I want to strip out
everything BUT the COLSPAN attribute.

The following strips out all attributes. What do I do if I want to keep a certain one?

eregi_replace(" <TD[^>]*>","<TD>", $string);


preg_replace is faster and more powerful

I tried this:
<?php

$data = '===<td a="b" colspan="3" x="y">===';

$regex = '<td([^>]*)( colspan=\S+)([^>]*)>';

$newdata = preg_replace("/$regex/i", '<td$2>', $data);

echo $newdata, "\n";

?>
The output was:
===<td colspan="3">===
HTH
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.


That does indeed strip out everything but the colspan! But how do I strip
out everything in TD tags that don't have the colspan at the same time?
Maybe a pattern that matches TD tags if they don't contain colspan?

I wish I knew this stuff...it's very useful.
Jul 17 '05 #4
jn

"Justin Koivisto" <sp**@koivi.com > wrote in message
news:Qg******** ********@news7. onvoy.net...
jn wrote:
I'm stripping out the attributes in <TD> tags...but I want to strip out
everything BUT the COLSPAN attribute.

The following strips out all attributes. What do I do if I want to keep a certain one?

eregi_replace(" <TD[^>]*>","<TD>", $string);
This is what I had used one time long ago:

preg_match_all( '/<td(\s([^>]+)*)>/i',$subject,$at tributes);

preg_match_all( '/[a-z]+\s*=\s*(\'|")? ([^\'"]*)\\1/i',$attributes[2][0],$attr
ibutes); $attributes=$at tributes[0];

I'm sure someone will have something better though...

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.


Thanks for the reply. That's pretty scary looking :)
Jul 17 '05 #5
> > $regex = '<td([^>]*)( colspan=\S+)([^>]*)>';

$newdata = preg_replace("/$regex/i", '<td$2>', $data);
That does indeed strip out everything but the colspan! But how do I strip
out everything in TD tags that don't have the colspan at the same time?
Maybe a pattern that matches TD tags if they don't contain colspan?
The above regex is very elegant. If you add a ? after the second regex it
will make matching the colspan optional. This can be problematic in terms
of what gets assigned to $1 and $2, so you can add ?: to those previous
patterns to suppress matching, and then use $1, which should be either the
colspan statement of null (but I haven't tested it, so I don't guarantee
it).
So the new regex would be:
$regex = '<td(?:[^>]*)( colspan=\S+)?(? :[^>]*)>';
$newdata = preg_replace("/$regex/i", '<td$1>', $data);

Another approach is to use preg_replace_ca llback:
http://us4.php.net/manual/en/functio...e-callback.php
I wish I knew this stuff...it's very useful. I highly recommend the book Mastering Regular Expressions, by Jeffrey
Friedl. It's very easy to ready and really gets you understand regexes.

Cheers,

Eric
"jn" <js******@cfl.r r.com> wrote in message
news:hN******** **************@ twister.tampaba y.rr.com... "Pedro" <he****@hotpop. com> wrote in message
news:bo******** *****@ID-203069.news.uni-berlin.de...
jn wrote:
I'm stripping out the attributes in <TD> tags...but I want to strip out everything BUT the COLSPAN attribute.

The following strips out all attributes. What do I do if I want to
keep a certain one?

eregi_replace(" <TD[^>]*>","<TD>", $string);


preg_replace is faster and more powerful

I tried this:
<?php

$data = '===<td a="b" colspan="3" x="y">===';

$regex = '<td([^>]*)( colspan=\S+)([^>]*)>';

$newdata = preg_replace("/$regex/i", '<td$2>', $data);

echo $newdata, "\n";

?>
The output was:
===<td colspan="3">===
HTH
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.


That does indeed strip out everything but the colspan! But how do I strip
out everything in TD tags that don't have the colspan at the same time?
Maybe a pattern that matches TD tags if they don't contain colspan?

I wish I knew this stuff...it's very useful.

Jul 17 '05 #6
jn

"Eric Ellsworth" <s@n> wrote in message
news:U4******** ************@sp eakeasy.net...
$regex = '<td([^>]*)( colspan=\S+)([^>]*)>';

$newdata = preg_replace("/$regex/i", '<td$2>', $data);
That does indeed strip out everything but the colspan! But how do I strip out everything in TD tags that don't have the colspan at the same time?
Maybe a pattern that matches TD tags if they don't contain colspan?


The above regex is very elegant. If you add a ? after the second regex it
will make matching the colspan optional. This can be problematic in terms
of what gets assigned to $1 and $2, so you can add ?: to those previous
patterns to suppress matching, and then use $1, which should be either the
colspan statement of null (but I haven't tested it, so I don't guarantee
it).
So the new regex would be:
$regex = '<td(?:[^>]*)( colspan=\S+)?(? :[^>]*)>';
$newdata = preg_replace("/$regex/i", '<td$1>', $data);

Another approach is to use preg_replace_ca llback:
http://us4.php.net/manual/en/functio...e-callback.php
I wish I knew this stuff...it's very useful.

I highly recommend the book Mastering Regular Expressions, by Jeffrey
Friedl. It's very easy to ready and really gets you understand regexes.

Cheers,

Eric
"jn" <js******@cfl.r r.com> wrote in message
news:hN******** **************@ twister.tampaba y.rr.com...
"Pedro" <he****@hotpop. com> wrote in message
news:bo******** *****@ID-203069.news.uni-berlin.de...
jn wrote:
> I'm stripping out the attributes in <TD> tags...but I want to strip out > everything BUT the COLSPAN attribute.
>
> The following strips out all attributes. What do I do if I want to

keep
a
> certain one?
>
> eregi_replace(" <TD[^>]*>","<TD>", $string);

preg_replace is faster and more powerful

I tried this:
<?php

$data = '===<td a="b" colspan="3" x="y">===';

$regex = '<td([^>]*)( colspan=\S+)([^>]*)>';

$newdata = preg_replace("/$regex/i", '<td$2>', $data);

echo $newdata, "\n";

?>
The output was:
===<td colspan="3">===
HTH
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.


That does indeed strip out everything but the colspan! But how do I

strip out everything in TD tags that don't have the colspan at the same time?
Maybe a pattern that matches TD tags if they don't contain colspan?

I wish I knew this stuff...it's very useful.



Thanks, but it stripped out everything, including the colspan. I'll try to
tinker with it and see if I can get it to work though.

Jul 17 '05 #7
Eric Ellsworth wrote:
So the new regex would be:
$regex = '<td(?:[^>]*)( colspan=\S+)?(? :[^>]*)>';
Maybe regex's aren't the best way to do this ... however I *had* to
manage it. Here it is for your enjoyment:

<?php
$s = ''; ### test data
$s.= 'CS ===<td a="b" color="blue" colspan="3" x="y">===' . "\n";
$s.= ' ===<td a="b" color="blue" rowspan="3" x="y">===' . "\n";
$s.= 'CS ===<td a="b" colspan="3" x="y">===' . "\n";
$s.= ' ===<td a="b" rowspan="3" x="y">===' . "\n";
$s.= 'CS ===<td colspan="3" x="y">===' . "\n";
$s.= ' ===<td rowspan="3" x="y">===' . "\n";
$s.= 'CS ===<td a="b" colspan="3">=== ' . "\n";
$s.= ' ===<td a="b" rowspan="3">=== ' . "\n";
$s.= 'CS ===<td colspan="3">=== ' . "\n";
$s.= ' ===<td rowspan="3">=== ' . "\n";
$s.= ' ===<td>===' . "\n";
$s.= ' ====== :)' . "\n";

$cs = '( colspan=[0-9\'"]+)?'; # optional " colspan=" followed by one or more digits or quotes
$ns = '(?:(?! colspan=[0-9\'"]+) \S+)*'; # zero or more, not grabbed *NOT* colspan
# ^^^------------------^ negative lookahead assertion

$regex = "<td$cs$ns$cs$n s$cs>"; # colspan can be immediately after td,
# or in the middle of the
# parameters or at the last position
$newx = preg_replace("/$regex/i", '<td$1$2$3>', $s);

echo "original:\ n", $s, "\n\nchanged:\n ", $newx, "\n";
?>
Another approach is to use preg_replace_ca llback:
http://us4.php.net/manual/en/functio...e-callback.php


And not learn the "negative lookahead assertion"? :-))
This was a very challenging challenge!

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #8
jn

"Pedro" <he****@hotpop. com> wrote in message
news:bo******** *****@ID-203069.news.uni-berlin.de...
Eric Ellsworth wrote:
So the new regex would be:
$regex = '<td(?:[^>]*)( colspan=\S+)?(? :[^>]*)>';
Maybe regex's aren't the best way to do this ... however I *had* to
manage it. Here it is for your enjoyment:

<?php
$s = ''; ### test data
$s.= 'CS ===<td a="b" color="blue" colspan="3" x="y">===' . "\n";
$s.= ' ===<td a="b" color="blue" rowspan="3" x="y">===' . "\n";
$s.= 'CS ===<td a="b" colspan="3" x="y">===' . "\n";
$s.= ' ===<td a="b" rowspan="3" x="y">===' . "\n";
$s.= 'CS ===<td colspan="3" x="y">===' . "\n";
$s.= ' ===<td rowspan="3" x="y">===' . "\n";
$s.= 'CS ===<td a="b" colspan="3">=== ' . "\n";
$s.= ' ===<td a="b" rowspan="3">=== ' . "\n";
$s.= 'CS ===<td colspan="3">=== ' . "\n";
$s.= ' ===<td rowspan="3">=== ' . "\n";
$s.= ' ===<td>===' . "\n";
$s.= ' ====== :)' . "\n";

$cs = '( colspan=[0-9\'"]+)?'; # optional " colspan=" followed by one or

more digits or quotes $ns = '(?:(?! colspan=[0-9\'"]+) \S+)*'; # zero or more, not grabbed *NOT* colspan # ^^^------------------^ negative lookahead assertion

$regex = "<td$cs$ns$cs$n s$cs>"; # colspan can be immediately after td,
# or in the middle of the
# parameters or at the last position
$newx = preg_replace("/$regex/i", '<td$1$2$3>', $s);

echo "original:\ n", $s, "\n\nchanged:\n ", $newx, "\n";
?>
Another approach is to use preg_replace_ca llback:
http://us4.php.net/manual/en/functio...e-callback.php


And not learn the "negative lookahead assertion"? :-))
This was a very challenging challenge!

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.


That was interesting :)

What I'm really doing is pasting from Excel into an "HTML Area" (
www.interactivetools.com). It's like a text area, but it's a little WYSIWYG
editor for content management systems. I'm stripping out all of the style
garbage Excel puts in its code, and replacing it with cleaned code. It works
great now, but I can't get it to preserve colspans because those get
stripped too.

I'll try some more things. Maybe I'll get it to work :)

Thanks guys
Jul 17 '05 #9
"jn" <js******@cfl.r r.com> wrote in message news:<TU******* *************** @twister.tampab ay.rr.com>...
"Pedro" <he****@hotpop. com> wrote in message
news:bo******** *****@ID-203069.news.uni-berlin.de...
Eric Ellsworth wrote:
So the new regex would be:
$regex = '<td(?:[^>]*)( colspan=\S+)?(? :[^>]*)>';

What I'm really doing is pasting from Excel into an "HTML Area" (
www.interactivetools.com). It's like a text area, but it's a little WYSIWYG
editor for content management systems. I'm stripping out all of the style
garbage Excel puts in its code, and replacing it with cleaned code. It works
great now, but I can't get it to preserve colspans because those get
stripped too.

I'll try some more things. Maybe I'll get it to work :)


Try http://weitz.de/regex-coach

---
"Learn from yesterday, live for today, hope for tomorrow. The
important thing is to not stop questioning."---Albert Einstein
Email: rrjanbiah-at-Y!com
Jul 17 '05 #10

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

Similar topics

5
2536
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL, and then reconstruct another URL based on it. For example, I need to scan a web page looking for something like <a href="some_dir/list_20050815100225.csv">. I don't know in advance what the date/time in the file name will be. I need to take the...
4
3235
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
10
3041
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...
3
3223
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
7
3834
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
9
3363
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use an app call The Regulator, which makes it pretty easy to build and test regular expressions. As a warning, I'm real weak with regular expressions. Let's say my regular expression is:
3
2568
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5180
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
3
1844
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular expression that only returns part of a string. For Example I have a multi-line text fragment like below:
18
622
by: Lit | last post by:
Hi, I am looking for a Regular expression for a password for my RegExp ValidationControl Requirements are, At least 8 characters long. At least one digit At least one upper case character
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
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,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
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...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
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
5552
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.