Jump to content

Why is my "ereg_replace" multiplying the replace value?


john_bboy7

Recommended Posts

Here is my Code:

 

$final_string = 'href="Something" href="Something" href="Something" href="Something" href="Something" href="Something"';
$link = 'http://www.example.com/';

preg_match_all('#(?<=href=")([^.]+)#', $final_string , $matches2);

foreach($matches2[0] as $val1){
$final_string = ereg_replace( $val1, $link.$val1, $final_string);
}

echo $final_string;

 

This is the result i get:

 

1st link = http://www.example.com/Something

2nd link = http://www.example.com/http://www.example.com/Something

3rd link = http://www.example.com/http://www.example.com/http://www.example.com/Something

and so on...

 

What i am trying to do is replace all the links in the page:

 

href="Something"

 

with:

 

href="http://www.example.com/Something"

 

Help will be appreciated.. :)

Why are you using ereg_replace? It is depreciated.

 

You should just use preg_replace instead.

 

$final_string = 'href="Something" href="Something1" href="Something2" href="Something3" href="Something4" href="Something5"';
$link = 'http://www.example.com/';

$final_string = preg_replace('#(?<=href=")([^.]+)#U', $link . '$1', $final_string);
echo $final_string;

 

Should do what you want.

 

OUTPUT:
href="http://www.example.com/Something" href="http://www.example.com/Something1" href="http://www.example.com/Something2" href="http://www.example.com/Something3" href="http://www.example.com/Something4" href="http://www.example.com/Something5"

Why are you using ereg_replace? It is depreciated.

 

You should just use preg_replace instead.

 

$final_string = 'href="Something" href="Something1" href="Something2" href="Something3" href="Something4" href="Something5"';
$link = 'http://www.example.com/';

$final_string = preg_replace('#(?<=href=")([^.]+)#U', $link . '$1', $final_string);
echo $final_string;

 

Should do what you want.

 

OUTPUT:
href="http://www.example.com/Something" href="http://www.example.com/Something1" href="http://www.example.com/Something2" href="http://www.example.com/Something3" href="http://www.example.com/Something4" href="http://www.example.com/Something5"

 

Wow... why didn't i thought of that..

 

Thanks man you are genius...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.