473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3231
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,$attributes);
preg_match_all('/[a-z]+\s*=\s*(\'|")?([^\'"]*)\\1/i',$attributes[2][0],$attributes);
$attributes=$attributes[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,$attributes);

preg_match_all('/[a-z]+\s*=\s*(\'|")?([^\'"]*)\\1/i',$attributes[2][0],$attr
ibutes); $attributes=$attributes[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_callback:
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.rr.com> wrote in message
news:hN**********************@twister.tampabay.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********************@speakeasy.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_callback:
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.rr.com> wrote in message
news:hN**********************@twister.tampabay.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$ns$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_callback:
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$ns$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_callback:
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.rr.com> wrote in message news:<TU**********************@twister.tampabay.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
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,...
4
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...
10
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...
3
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...
7
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...
9
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...
3
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...
25
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...
3
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...
18
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.