replace all _self with _blank

This is a discussion on replace all _self with _blank within the PHP Language forums, part of the PHP Programming Forums category; Hi! I have a string containing a HMTL page.I need to insert target="_blank" where targer attribute ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-02-2006
bboldi
 
Posts: n/a
Default replace all _self with _blank

Hi!

I have a string containing a HMTL page.I need to insert target="_blank"
where targer attribute is not present within <a... TAG, and replace
other target attributes - example target="_self" - to _blank. Can
anyone help me? Thanx in advance, and sorry for my english.

Reply With Quote
  #2 (permalink)  
Old 03-02-2006
Marcin Dobrucki
 
Posts: n/a
Default Re: replace all _self with _blank

bboldi wrote:
> Hi!
>
> I have a string containing a HMTL page.I need to insert target="_blank"
> where targer attribute is not present within <a... TAG, and replace
> other target attributes - example target="_self" - to _blank. Can
> anyone help me? Thanx in advance, and sorry for my english.


how about:
http://www.php.net/manual/en/function.str-replace.php
http://php.net/preg_replace
Reply With Quote
  #3 (permalink)  
Old 03-02-2006
bboldi
 
Posts: n/a
Default Re: replace all _self with _blank

Ok. I know about these functions, the problem is: i need to determine
if target attribute is alredy defined, if not, i need to insert one, if
yes, then replace the variable of target attribute i have problen with
"finding if target att. is defined and inserting one if not" part. any
idea? thanx in advance.

Reply With Quote
  #4 (permalink)  
Old 03-06-2006
Marcin Dobrucki
 
Posts: n/a
Default Re: replace all _self with _blank

bboldi wrote:
> Ok. I know about these functions, the problem is: i need to determine
> if target attribute is alredy defined, if not, i need to insert one, if
> yes, then replace the variable of target attribute i have problen with
> "finding if target att. is defined and inserting one if not" part. any
> idea? thanx in advance.


I hope this is not some homework...

if (preg_match("target", $url)) {
// search and replace
}
else {
// crude but effective, url in form:
// <a href="someplace.html" option="value">link text</a>
$url_array = explode('>', $url);
$new_url = $url_array[0].' target="your_target">'.$url_array[1] . '>';
}

To do a bit more work, search for the position of the first '>', and
then insert a string into the url (eg. with stripos() and
substr_replace()) just before that place.

Of if you really want to put effort in, tokenize the whole url, and
then parse the arguments as you see fit.

/Marcin
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:18 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0