john_bboy7 Posted July 3, 2010 Share Posted July 3, 2010 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.. Link to comment https://forums.phpfreaks.com/topic/206632-why-is-my-ereg_replace-multiplying-the-replace-value/ Share on other sites More sharing options...
premiso Posted July 3, 2010 Share Posted July 3, 2010 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" Link to comment https://forums.phpfreaks.com/topic/206632-why-is-my-ereg_replace-multiplying-the-replace-value/#findComment-1080734 Share on other sites More sharing options...
john_bboy7 Posted July 3, 2010 Author Share Posted July 3, 2010 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... Link to comment https://forums.phpfreaks.com/topic/206632-why-is-my-ereg_replace-multiplying-the-replace-value/#findComment-1080742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.