SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Better preg_replace?

    Hi all,

    Is there a more elegant way to write this line?
    Code:
    $string = preg_replace('#\[tag\](https?://www\.|https?://)(.*)\[/tag\]#siU', '\\2', $string);
    Basically, I want to replace any http://www, https://www, http:// and https:// instances included into [tag][/tag] with nothing.
    Thanks.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Depends on what you mean by elegent, really.

    Here's what I would use:
    PHP Code:
    preg_replace('/\[tag\]https?:\/\//i''[tag]'$string); 
    I wouldn't bother with the www bit, because http://yoursite.com can be different to http://www.yoursite.com.

    But if you are really adement on the www being removed:
    PHP Code:
    preg_replace('/\[tag\](https?:\/\/)?(w{3}\.)?(.*?)\[\/tag\]/i''[tag]\3[/tag]'$string); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to make sure the www is removed also.
    In my case, it does not matter if the site will behave differently because I will not use the replaced bits as a link. Thanks.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Hopefully the following might help. It should (in theory) behave as you requested in the first post.

    PHP Code:
    // Split onto multiple lines because of Sitepoint's
    // silly narrow code highlighting box
    preg_replace(
        
    '~\[tag\]https?://(?:www\.)?(.*?)\[/tag\]~is',
        
    '$1',
        
    $string
    ); 
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Hopefully the following might help. It should (in theory) behave as you requested in the first post.

    PHP Code:
    // Split onto multiple lines because of Sitepoint's
    // silly narrow code highlighting box
    preg_replace(
        
    '~\[tag\]https?://(?:www\.)?(.*?)\[/tag\]~is',
        
    '$1',
        
    $string
    ); 
    It behaves, partially.
    Your preg_replace will return www.domain.com, instead of domain.com. It removes the http:// only.

    My variant works well, but I just want to find a way to write it shorter...
    Still learning this regex stuff. Thanks.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    I've probably overlooked something simple, but did you try it out? The following behaves as advertised:
    PHP Code:
    <?php

    header
    ('Content-Type: text/plain;charset=utf-8');
    error_reporting(E_ALL E_STRICT);

    $tests = array (
        
    '[tag]https://www.example.org/example.html[/tag]',
        
    'this is [tag]http://cool beans[/tag]',
        
    'Hmm, does this work? [tag]http://www.Yes![/tag]'
    );

    foreach (
    $tests as $string)
    {
        echo 
    preg_replace(
            
    '~\[tag\]https?://(?:www\.)?(.*?)\[/tag\]~is',
            
    '$1',
            
    $string
        
    ), "\n";
    }
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •