0

I have a variable that contains the following text in PhP:

$description = "Flash <a href=\"http://www.aaa.com/\">this coupon</a> <br > <a href="http://www.ggg.com/">Visit here</a> for details <br >";

How do i add the

target="_blank"

that missing on the $description variable?

So the result will becomes the following:

$description = "Flash <a href=\"http://www.aaa.com/\" target="_blank">this coupon</a> <br > <a href="http://www.ggg.com/" target="_blank">Visit here</a> for details <br >";

I am looking for solution that the variable $description was recorded by user accidentally. But I need the system to find out and correct it by adding the target="blank" into the existing variable and update the record.

Ray
  • 139
  • 2
  • 5
  • 15
  • What is your question? Why can't you do it exactly as you've outlined here? – brbcoding Mar 28 '13 at 12:16
  • You can do it exactly as you said, but you should escape your quotes (even in your first example) or maybe consider wrapping it in single quotes and use double quotes for the HTML properties. – Niek van der Steen Mar 28 '13 at 12:16
  • single quota inside single quota should be escaped with \, also same for double quota, you can use single quota inside double quota without any escape and vice versa –  Mar 28 '13 at 12:18
  • I think the question is about HOW, not knowing the URL of the `` he can inject the `target="_blank"` – Nick Andriopoulos Mar 28 '13 at 12:19
  • I am looking for solution that the variable $description was recorded by user accidentally. But I need the system to find out and correct it by adding the target="blank" into the existing variable and update the record. – Ray Mar 28 '13 at 12:20
  • That is more complicated since you need to check if the link already has this attribute, you could to this with a regular expression e.g. – kero Mar 28 '13 at 12:24
  • @user1109161 Take a look @ my post. I edited my answer. $string = preg_replace("//", "", $string); – eL-Prova Mar 28 '13 at 12:27

4 Answers4

20

I have no idea what your exactly meaning. However, a suggestion to use ' within your HTML. And only " for the echo. So it will be:

$description = "Flash <a href='http://www.aaa.com/' target='_blank'>this coupon</a><br ><a href='http://www.ggg.com/' target='_blank'>Visit here</a> for details <br >";

For adding target you can use this:

$string = preg_replace("/<a(.*?)>/", "<a$1 target=\"_blank\">", $string);

Found on: preg_replace links in text with <a> with some exception

Community
  • 1
  • 1
eL-Prova
  • 1,084
  • 11
  • 27
  • You should do it the other way around. `'` for the string and `"` for the html attributes. Although single quotes are allowed, double quotes are a bit more the "standard" way for html. – Marcel Gwerder Mar 28 '13 at 12:27
3
$description = str_replace('<a href="','<a target="_blank" href="',$description);
Narek
  • 3,813
  • 4
  • 42
  • 58
2

You already did it, but your code will produce errors since you didn't escape the " in the second link

$description = "Flash <a href=\"http://www.aaa.com/\" target=\"_blank\">this coupon</a> <br > <a href=\"http://www.ggg.com/\" target=\"_blank\">Visit here</a> for details <br >";

Alternatively you can use ' then you don't have to escape

$description = 'Flash <a href="http://www.aaa.com/" target="_blank">this coupon</a> <br > <a href="http://www.ggg.com/" target="_blank">Visit here</a> for details <br >';
kero
  • 10,647
  • 5
  • 41
  • 51
  • Sorry guys, maybe I have mislead my question. Have just updated the question again. – Ray Mar 28 '13 at 12:24
1

If you note that properties of tags do not have a specific order, you can simply replace <a href with <a target=\"_blank\" href

Nick Andriopoulos
  • 10,313
  • 6
  • 32
  • 56
  • hi @hexblot, thanks for the comment. Actually, I am looking for solution that the variable $description was recorded by user accidentally. But I need the system to find out and correct it by adding the target="blank" into the existing variable and update the record – Ray Mar 28 '13 at 12:21