Hi, I am trying to build a function which replaces a part of the url which a new url but cannot get it to work. For example below i have a string with a few images in like this: http://thebuildcompany.mysite.biz/uploads/images/532966935.PNG Code (markup): I what the function to look for the url and replace anything between http:// and the first / so the above will be changed to: http://thebuildcompany.co.uk/images/532966935.PNG Code (markup): Any help will be appreciated. <?php // include init which contains all the includes. include('init.php'); function formatUrlsInText($text, $newurl){ $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; preg_match_all($reg_exUrl, $text, $matches); $usedPatterns = array(); foreach($matches[0] as $pattern){ if(!array_key_exists($pattern, $usedPatterns)){ $usedPatterns[$pattern]=true; $text = str_replace ($pattern, "http://{$newurl}", $text); } } return $text; } $string = '<p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s. <img src="http://thebuildcompany.mysite.biz/uploads/images/thumbs/932d16935.PNG" width="250" align="right" style="margin: 0px 0px 0px 25px;"> when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><img src="http://thebuildcompany.mysite.biz/uploads/images/thumbs/532966935.PNG" width="250" align="right" style="margin: 0px 0px 0px 25px;"></p>'; print formatUrlsInText($string, "thebuildcompany.co.uk"); ?> Code (markup):
Instead of writing the code for wordpress project please use Search and Repalce plugin ... http://wordpress.org/plugins/search-and-replace/
Not sure if it's funny or sad that people directly assume the code you post on a message board is Wordpress... When you say "everything between http:// and the first /", wouldn't that imply that the new URL should be: http://thebuildcompany.co.uk/uploads/images/532966935.PNG Code (markup): ...? Is that a typo in the description of the problem, or the new URL? Anyway, give this a shot: function formatUrlsInText($text, $newURL) { return preg_replace( '~((?:ht|f)tps?://)(?:[^/]+)(/[\w-\./]*)~', '$1' . $newURL . '$2', $text ); } PHP:
Actually just found a small issue, i forgot to mention that i only want to change urls in the string which are sub domains that end in mysite.biz so e.g. http://thebuildcompany.mysite.biz i dont want to change any other URL's
You could wrap it in an "if"... $theurl = $_SERVER['HTTP_HOST']; // primary url $host1 = "subdomain1.mysite.biz"; // 1st subdomain $host2 = "subdomain2.mysite.biz"; // 2nd subdomain if($theurl== $host1 || $theurl==$host2 ||... ){ echo "subdomain detected - time for niko's function here";}else{ echo "this must be a primary domain";} Code (markup):
Thanks for the reply, the problem with this is that the URL could be one of a million so the if formula would be massive. It needs to dynamically check if the domain ends in mysite.biz and then change it.