Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

perl script replace links in text

2:24 pm on Oct 6, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 6, 2009
posts: 6
votes: 0

Hi i want to replace some links in some text they are either <img src="" /> and <a href="" />

i have these following path i want to replace:


with this virtual directory path


can anyone help me ?

8:38 pm on Oct 6, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

Welcome aboard marryshelly, typing this out on the fly and tested, it should get you started.

#!/usr/bin/perl -w
@tests = (
'This is an empty link <a href="">testme</a>.',
'This is a real link <a href="http://example.com">testme</a>.',
'This is a link using single quotes <a href=\'http://example.com\'>testme</a>.',
'This is a 4.01 image <img src="http://example.com/test.jpg">.',
'This is an XHTML image <img src="http://example.com/test.jpg" />.'
foreach $line (@tests) {
if ($line =~ /\<.*img[^>]+¦href[^>]+\s*\/*\>/i) {
$line =~ s/(<.*img[^"']+¦href[^"']+)(['"]*)([^"']*)(['"]*\s*\/*\>)/$1$2$my_replacement$4/ig;
print "New line is now \"$line\"\n";

Note that:
- This message board borks the pipe character, the one you get if you hold the shift key and type \. Be sure to replace the double-pipes in the code above with the actual pipe.

- Use XHTML syntax in your code only if you require XHTML features, and your server outputs text/xhtml headers, most output text/html by default (i.e., don't misrepresent your document type.)

[edited by: phranque at 9:21 pm (utc) on Oct. 6, 2009]
[edit reason] disabled graphic smileys ;) [/edit]

7:40 am on Oct 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 6, 2009
votes: 0

Hi rocknbil, thanks for the quick reply it realy helps me to fix my problem.
7:55 am on Oct 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 6, 2009
votes: 0

I have another issue regarding my site which has virtual directories.

it has
en = english
es = spanish
de = german


What can i do if i am living on a directory which is mensioned above with different root levels with english spanish etc.

How to add that to existing links so
the link i have with (img or a)

to look like this

I will be pleased if someone could help me out with this!

i have to also mention that links are in some Text that i want to modify!

12:25 pm on Oct 7, 2009 (gmt 0)


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 61

welcome to WebmasterWorld [webmasterworld.com], marryshelly!

it would be easier to write a simple yet suitable regular expression for your problem if you described in more detail what the text patterns look like.
for example:
are you always using double quotes for quoting your href/src attribute values?
are the links in the "text you want to modify" fully qualified urls, as in http://(subdomain.)?example.tld/path and if the protocol specifier isn't specified how would you be able to recognize it as a url?

have you looked at the regular expression references and tutorials linked in the Perl Server Side CGI Scripting forum Charter [webmasterworld.com]?
while you are there note the item about "Do My Homework posts".
it would be preferable if you showed your best attempt at coding this and described the incorrect results.

1:21 pm on Oct 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 6, 2009
votes: 0

As rocknbil showed in his example, it has some text and that text string can hold some hyperlinks and images which i want to change the paths for.

some @text = "<p>Some text in the editor <a href="/content/images/background.gif" target="background">/content/images/background.gif</a></p>
<p><img height="289" alt="" src="/content/images/background.gif" width="786" border="0"></p>

And yes the link attributes are double quoted and they do not hold fully qualified urls but root level paths as in the @text.

can someone shed some light on this?

1:54 pm on Oct 7, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 6, 2009
votes: 0

The problem is that, the site is generated in CMS system which does not know the site struture.

if we say my site looks like http://www.example.com/ with virtual directories (en, en, de). And every directory has there own language content.

for example if you goto my site you will be able to chose different language content for different countries.

so it will look like this
http://www.example.com/en/ - english
http://www.example.com/es/ - spanish
http://www.example.com/de/ - german

When i generate different content in my CMS system and somehow add language to my root level "en/content/etc"?

any idea how to ?

[edited by: phranque at 9:44 pm (utc) on Oct. 7, 2009]
[edit reason] examplified domains [/edit]

1:54 pm on Oct 12, 2009 (gmt 0)

New User

5+ Year Member

joined:Oct 6, 2009
votes: 0

System: The following message was spliced on to this thread from: http://www.webmasterworld.com/perl/4005548.htm [webmasterworld.com] by phranque - 3:09 pm on Oct. 27, 2009 (utc -7)

Hello, anyone who can help me ?

i have this

my $tests = "some text with links <a href="/design/images/default.aspx" > Link </a> and some images in this string <img src="\design\images\dog.jpg" />";

my $pattern = array(" href="\/"," src="\/")
my $replacement = array(" runat="server" href="~\/"," runat="server" src="~\/");

my $result = preg_replace($pattern,$replacement, $tests);

print $result;



how can i replace a link with runat="server" attribute and add this to href="~\" ? any solutions ? anyone who can guide me?=?


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members