Connecting Tech Pros Worldwide Help | Site Map

Just a quick regular expression question!

gene.ellis@gmail.com
Guest
 
Posts: n/a
#1: Dec 8 '05
Hello everyone,

This should be extremely simple for youregular expression gurus out
there. I am looking for a regular expression that will look for the
hyperlinks within a string, and then add some simple code before the
hyperlinks, but only if they do not match a certain URL. For example.

$string ="This is a <a href='http://www.ur1.com'>Hyperlink</a> and this
is a <a href='http://www.ur2.com'> Hyperlink</a>";

I would like to a regular expression to look in the string for
hyperlinks that start with "http://www.ur1" and change add some code to
it like so that the string is modified like:

$string ="This is a <a
href=javascript:'http://www.ur1.com'>Hyperlink</a> and this is a <a
href='http://www.ur2.com'> Hyperlink</a>";

That's it. Thank you very much for your help!

Sjoerd
Guest
 
Posts: n/a
#2: Dec 8 '05

re: Just a quick regular expression question!


Couldn't you find anything on Google, for example by searching on
"regex url"? If you want to append .com to links, try using
preg_replace.

bala
Guest
 
Posts: n/a
#3: Dec 9 '05

re: Just a quick regular expression question!


Hi Gene...

Try this....if u have found any pbm ,mail me

$string ="This is a <a href='http://www.ur1.com'>Hyperlink</a> and this

is a <a href='http://www.ur2.com'> Hyperlink</a>";

echo 'Before => '.$string;

$string = ereg_replace('(.*)(http:\/\/www.ur1)(.*)',
'\\1javascript:\\2\\3', $string);

echo '<br>After => '.$string;

gene.ellis@gmail.com
Guest
 
Posts: n/a
#4: Dec 16 '05

re: Just a quick regular expression question!


Thanks! Just one more querstion. What if I wanted to match all
hyperlinks that DO NOT match a certain domain. Such as, what if I
wanted to find all URLS that do not match http://www.url1

gene.ellis@gmail.com
Guest
 
Posts: n/a
#5: Dec 22 '05

re: Just a quick regular expression question!


so this is my regular expression:


$string_sep=preg_match_all('/(http:\/\/.+\.com)/',$page_content,$match,PREG_SET_ORDER);
for ($i = 0; $i<count($match); $i++) {
$url = $match[$i][0];
ereg('http:\/\/www\.(.*)\.com', $url, $out);
if ($out[1] != 'uctltc') {
// echo $url.'<br>';
$page_content =
ereg_replace("$url","javascript:launch('http://www.uctltc.org/common/opener.php?url=$url','window1')","$page_content");
}
}

It works! However, I need it to not only look through .com URLS, but be
open to the other extensions as well (.org, .net, .etc). And I want it
to only exampline hyperlink URLS, not image URLS, or any other sort of
URL. Thanks so much for your help! It is almost done!

Closed Thread