to add extra property to a hyperlink?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
rajaseeth
Forum Newbie
Posts: 4
Joined: Tue Nov 03, 2009 9:27 am

to add extra property to a hyperlink?

Post by rajaseeth »

Hi,
I want to identify hyperlink tags in a string and then add a property "target" to that hyperlink. for example i have <a href ="http://www.google.com">google.com</a>. i want to add a property "target" to the above hyperlink like <a href ="http://www.google.com" target="_new">google.com</a>. I am new to programming, so please help me out..

Thanks
Raja
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: to add extra property to a hyperlink?

Post by JakeJ »

well, when you construct your URL, just concatinate different strings.

$a = 'Hello';

$b = 'World';

$c = $a. ' '.$b;

Echo $c;

Result: Hello World

echo $b;
//output is Hello world
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: to add extra property to a hyperlink?

Post by ridgerunner »

Try this:

Code: Select all

$contents = preg_replace('/<a\b([^>]*)>/', '<a$1 target="_new">', $contents);
:)
Post Reply