By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

preg_replace help: removing target="_blank" from links

P: n/a
I admit, I'm terrible creating reg ex's.

I'm trying to create a preg_replace that would remove from a <a href> tag
that would replace the target attribute regardless of what the value might
be.

Any ideas?
--
[ Sugapablo ]
[ http://www.sugapablo.net <--personal | http://www.sugapablo.com <--music ]
[ http://www.2ra.org <--political | http://www.subuse.net <--discuss ]

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Sugapablo wrote:
I admit, I'm terrible creating reg ex's.

I'm trying to create a preg_replace that would remove from a <a href> tag
that would replace the target attribute regardless of what the value might
be.

Any ideas?

$foo = '<a href="http://www.foo.com" target="_blank">www.foo.com</a>';
$foo = preg_replace('/(<a.*?)[ ]?target="_blank"(.*?)/', '$1$2', $foo);

That should work.

Joe Estock
Jul 17 '05 #2

P: n/a
On Tue, 12 Apr 2005 13:41:09 +0000, Joe Estock wrote:
$foo = '<a href="http://www.foo.com" target="_blank">www.foo.com</a>';
$foo = preg_replace('/(<a.*?)[ ]?target="_blank"(.*?)/', '$1$2', $foo);

That should work.


Actually, now that I think about it, what about a preg_replace that would
replace any attribute except for the href attribure?

--
[ Sugapablo ]
[ http://www.sugapablo.net <--personal | http://www.sugapablo.com <--music ]
[ http://www.2ra.org <--political | http://www.subuse.net <--discuss ]

Jul 17 '05 #3

P: n/a
Sugapablo wrote:
On Tue, 12 Apr 2005 13:41:09 +0000, Joe Estock wrote:

$foo = '<a href="http://www.foo.com" target="_blank">www.foo.com</a>';
$foo = preg_replace('/(<a.*?)[ ]?target="_blank"(.*?)/', '$1$2', $foo);

That should work.

Actually, now that I think about it, what about a preg_replace that would
replace any attribute except for the href attribure?

preg_replace('/(<a href=).*?(.*?)>(.*?)/', '$1$2$3', $text); should do
it for you.

Joe Estock
Jul 17 '05 #4

P: n/a
On Tue, 12 Apr 2005 14:11:12 +0000, Joe Estock wrote:
preg_replace('/(<a href=).*?(.*?)>(.*?)/', '$1$2$3', $text); should do
it for you.


No, sorry.

This:
<?php

$text = "<a href=\"test.html\" target=\"_blank\">test</a>";

echo preg_replace('/(<a href=).*?(.*?)>(.*?)/', '$1$2$3', $text);

?>

produced this:
<a href="test.html" target="_blank"test></a>

--
[ Sugapablo ]
[ http://www.sugapablo.net <--personal | http://www.sugapablo.com <--music ]
[ http://www.2ra.org <--political | http://www.subuse.net <--discuss ]

Jul 17 '05 #5

P: n/a
Sugapablo wrote:
On Tue, 12 Apr 2005 13:41:09 +0000, Joe Estock wrote:

$foo = '<a href="http://www.foo.com" target="_blank">www.foo.com</a>';
$foo = preg_replace('/(<a.*?)[ ]?target="_blank"(.*?)/', '$1$2', $foo);

That should work.


Actually, now that I think about it, what about a preg_replace that would
replace any attribute except for the href attribure?


Try something like this (untested):

$pattern='`<a\s+[^>]*(href=([\'\"]).*\\2)[^>]*>([^<]*)</a>`isU';
$string=preg_replace($pattern,"<a $1>$3</a>",$string);

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #6

P: n/a
Sugapablo wrote:
On Tue, 12 Apr 2005 14:11:12 +0000, Joe Estock wrote:

preg_replace('/(<a href=).*?(.*?)>(.*?)/', '$1$2$3', $text); should do
it for you.

No, sorry.

This:
<?php

$text = "<a href=\"test.html\" target=\"_blank\">test</a>";

echo preg_replace('/(<a href=).*?(.*?)>(.*?)/', '$1$2$3', $text);

?>

produced this:
<a href="test.html" target="_blank"test></a>

sorry, that should have been preg_replace('/(<a href=".*?").*?(>.*?)/',
'$1$2', $text);
Jul 17 '05 #7

P: n/a
Joe Estock <je*****@NOSPAMnutextonline.com> writes:

[remove all attributes except href from anchor tag]
sorry, that should have been preg_replace('/(<a
href=".*?").*?(>.*?)/', '$1$2', $text);


This, along with every other solution thus far posted, relies on href
being the first attribute. Parsing HTML with regexes is extremely
painful and much better suited to an HTML Parser. Unfortunately, I've
never used an HTML Parser in PHP (AFAIK, one was not available until
about a year ago, which is actually kind of ironic).

See <URL:http://us3.php.net/tidy>. Good luck though, there doesn't
seem to be much documentation. There's a tutorial here:
<URL:http://www.zend.com/php5/articles/php5-tidy.php>.
Jul 17 '05 #8

P: n/a
Steven Vasilogianis wrote:
This, along with every other solution thus far posted, relies on href
being the first attribute. Parsing HTML with regexes is extremely
painful and much better suited to an HTML Parser. Unfortunately, I've
never used an HTML Parser in PHP (AFAIK, one was not available until
about a year ago, which is actually kind of ironic).


Um... did you even look at the solution I posted?

`<a\s+[^>]*(href=([\'\"]).*\\2)[^>]*>([^<]*)</a>`isU'
^^^^^

It only requires that href is there, and it is not case-sensitive, AND
the link doesn't need to be completely on one line...

--
Justin Koivisto - ju****@koivi.com
http://koivi.com
Jul 17 '05 #9

P: n/a
Justin Koivisto <ju****@koivi.com> writes:
Steven Vasilogianis wrote:
This, along with every other solution thus far posted, relies on href
being the first attribute. [...]
Um... did you even look at the solution I posted?


Not closely enough, apparently :-(.
`<a\s+[^>]*(href=([\'\"]).*\\2)[^>]*>([^<]*)</a>`isU'
^^^^^

It only requires that href is there, and it is not case-sensitive, AND
the link doesn't need to be completely on one line...


There are still situations where your regex will break[1] (admittedly,
many of them are awfully contrived). I still maintain that regular
expressions are not very well suited for parsing HTML.

I have to admit though, as far as regexes for parsing HTML can go,
that's a pretty good one.

[1] Various combinations of < and/or >'s in attribute values, and/or as anchor
text.
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.