473,796 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_replace help: removing target="_blank" from links

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
9 7687
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
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
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
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.htm l\" 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
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_re place($pattern, "<a $1>$3</a>",$string);

--
Justin Koivisto - ju****@koivi.co m
http://koivi.com
Jul 17 '05 #6
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.htm l\" 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
Joe Estock <je*****@NOSPAM nutextonline.co m> 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
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.co m
http://koivi.com
Jul 17 '05 #9
Justin Koivisto <ju****@koivi.c om> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3320
by: Romain | last post by:
Hi, I've used an excellent way to make target="_blank" links that still validate while using HTML 4.01 Transitional doctypes in the past. I learned the trick from : http://www.sitepoint.com/article/1041 Basically, instead of typing : <a href="yourlink.html" target="_blank" alt="">The link</a>
16
3619
by: St. Rechsteiner | last post by:
Hi How i made the same effect in xHTML like the - target="_blank" - Tag in HTML 4.01? I will use it with xHTML and CSS ... but the validator say's to me, that this tag wasn't valid for xHTML! ... Why? Is there no "same effect"-Tag in xHTML? Thx for answers!
6
77511
by: Tony Marston | last post by:
The code <a href="..." target="_blank">...</a> will not validate as XHTML STRICT because of the 'target' tag, so how do I achieve the same result by moving it to a CSS file? I cannot find anything which allows me to specify 'target=' on an anchor tag. -- Tony Marston http://www.tonymarston.net
2
2845
by: Matt | last post by:
In the following code, page1.asp is inside the frame of main.html. When the user click submit button in page1.asp, it will submit the form and open a new window called page2.asp. When the user clicks submit button on page2.asp, I expected to open a new window called page3.asp. Unfortunately, it just open page3.asp in the same window as page2.asp, and now page2.asp is gone. However, if I just open page1.asp, and begins from there, it is...
8
3860
by: msnews.microsoft.com | last post by:
I want to redirect the user to a url outside of our website but I want it to preserve our application's window by opening a new window. We have a datagrid that has five hyperlink columns containing links to external sites. The hyperlink columns have the target="_blank" attribute so they preserve the existing window. This works OK except that the NavigateURL's are quite long due to the size of the query string and this makes the page slow. ...
5
3991
by: Jon via DotNetMonster.com | last post by:
<siteMapNode title="share price" description="Link to Netcall on the London Stock Exchange" url="http://www.yahoo.co.uk" role="" target="_blank" /> Hi all I'm trying to open the Yahoo web site in a new window using the above code in my asp.net web.sitemap. I'm expecting the target="_blank" bit to make it do this. but it isnt. Does anyone know why?? Thanks in advance.
5
9319
by: John A Grandy | last post by:
I have a js function that dynamically generates a block of html that includes an anchor tag : <a href="http://www.google.com" target="_blank" /> When the user clicks this link, the new IE6 browser window is full vertical size, but only half horizontal size (of available screen area). Starting iexplore.exe independently opens a full-size browser window.
28
8844
by: Jim Carlock | last post by:
For Strict DOCTYPEs, what's the recommended way to get an anchor to open inside a new browser? Thanks. Jim Carlock Post replies to the group.
6
6116
by: fran7 | last post by:
Hi, I know that <base target="_blank"> will open all links in a new window. Anyone can suggest how to use it but selectively not open all the links. I have 10 links out of 100 that I dont want to open in a new window. Thanks in advance richard
0
9533
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
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
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
7555
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
6796
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
5447
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
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
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.