1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Replace URL in String

Discussion in 'PHP' started by adamjblakey, Apr 28, 2014.

  1. #1
    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.
    SEMrush
    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):
     
    adamjblakey, Apr 28, 2014 IP
    SEMrush
  2. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #2
    salmanshafiq, Apr 28, 2014 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Sorry this is not for wordpress, this is just for a php script.
     
    adamjblakey, Apr 28, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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:
     
    Last edited: Apr 28, 2014
    nico_swd, Apr 28, 2014 IP
    malky66 and ryan_uk like this.
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Thank you very much, this works great.
     
    adamjblakey, Apr 29, 2014 IP
  6. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #6
    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
     
    adamjblakey, Apr 29, 2014 IP
  7. - jeff -

    - jeff - Member

    Messages:
    63
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    38
    #7
    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):
     
    - jeff -, Apr 29, 2014 IP
  8. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #8
    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.
     
    adamjblakey, Apr 30, 2014 IP